Structured Output

Generate structured JSON output from text prompts using specific language model. Generates structured JSON content from a given free-form prompt and an optional JSON schema. Uses language models to produce results that conform to user-defined constraints.

  • Endpoint: POST /structured-output

Headers

Authorization: Bearer ts_live_abc123...
Content-Type: application/json
Accept: application/json

Request Format

Request Body

{
  "prompt": "string, required",
  "schema": {
    "type": "object",
    "properties": {
      "your_field": { "type": "string" },
      "...": "..."
    },
    "required": ["..."],
    "additionalProperties": false
  }
}
  • Fields:

  • prompt (required): Natural language description of desired structured data

  • schema (optional): JSON schema defining expected response structure

Example Request

curl https://api.tokensource.com/v1/structured-output \
  -H "Authorization: Bearer ts_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Extract transaction details from: Wire transfer of $50,000 from Account #1234 (Savings) to Account #5678 (Checking) on March 15, 2024, with memo 'Business Expense Payment' and priority 'High'",
    "schema": {
      "type": "object",
      "properties": {
        "amount": {"type": "number"},
        "source": {
          "type": "object",
          "properties": {
            "account_number": {"type": "string"},
            "account_type": {"type": "string", "enum": ["Checking", "Savings", "Business"]}
          }
        },
        "destination": {
          "type": "object",
          "properties": {
            "account_number": {"type": "string"},
            "account_type": {"type": "string", "enum": ["Checking", "Savings", "Business"]}
          }
        },
        "date": {"type": "string", "format": "date"},
        "memo": {"type": "string"},
        "priority": {"type": "string", "enum": ["Low", "Medium", "High"]}
      },
      "required": ["amount", "source", "destination", "date", "memo", "priority"]
    }
  }'

Responses

Successful Response (200 OK)

{
  "amount": 50000.00,
  "source": {
    "account_number": "1234",
    "account_type": "Savings"
  },
  "destination": {
    "account_number": "5678",
    "account_type": "Checking"
  },
  "date": "2024-03-15",
  "memo": "Business Expense Payment",
  "priority": "High"
}

Error Responses

400 Bad Request

{
  "error": "Invalid input",
  "details": [
    {
      "field": "prompt",
      "message": "Prompt must not be empty."
    }
  ]
}

401 Unauthorized

{
  "error": "Unauthorized",
  "message": "Invalid or missing API key."
}

500 Internal Server Error

{
  "error": "Internal server error",
  "message": "An unexpected error occurred."
}

Advanced Usage

Complex Schema Validation

{
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "id": { "type": "string", "pattern": "^[0-9a-f]{24}$" },
        "email": { "type": "string", "format": "email" },
        "age": { "type": "integer", "minimum": 18, "maximum": 120 },
        "preferences": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["sports", "music", "movies", "books"]
          },
          "minItems": 1,
          "uniqueItems": true
        }
      },
      "required": ["id", "email"]
    }
  }
}

Examples

1. Compliance Report Generation

Request:

curl -X POST https://api.tokensource.com/v1/structured-output \
  -H "Authorization: Bearer ts_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Generate a compliance report for these transactions: [Last 24 hours: 15 transactions, Total volume: 500,000 USDT, Max single transaction: 100,000 USDT, Unique addresses: 8]. Flag any suspicious patterns or compliance concerns.",
    "schema": {
      "type": "object",
      "properties": {
        "transaction_patterns": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "pattern_type": {"type": "string"},
              "risk_assessment": {"type": "string"},
              "recommended_actions": {"type": "array", "items": {"type": "string"}}
            }
          }
        },
        "regulatory_flags": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["aml_concern", "kyc_incomplete", "high_volume", "suspicious_pattern"]
          }
        }
      }
    }
  }'

Response:

{
  "transaction_patterns": [
    {
      "pattern_type": "High single transaction volume",
      "risk_assessment": "Medium",
      "recommended_actions": [
        "Review transaction details",
        "Conduct enhanced due diligence",
        "Flag for compliance review"
      ]
    }
  ],
  "regulatory_flags": ["high_volume", "suspicious_pattern"]
}

2. Transaction Analysis

Request:

curl -X POST https://api.tokensource.com/v1/structured-output \
  -H "Authorization: Bearer ts_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Analyze this transaction: Amount: 50000 USDT, From: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e, To: 0x123...789, Timestamp: 2024-03-15 14:30 UTC, Purpose: '\''Business settlement for Q1 services'\''",
    "schema": {
      "type": "object",
      "properties": {
        "category": {
          "type": "string",
          "enum": ["trading", "payment", "settlement", "fees", "other"]
        },
        "risk_level": {
          "type": "string",
          "enum": ["low", "medium", "high"]
        },
        "purpose": {
          "type": "string",
          "maxLength": 200
        },
        "involved_entities": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {"type": "string"},
              "role": {"type": "string"},
              "risk_factors": {"type": "array", "items": {"type": "string"}}
            }
          }
        }
      }
    }
  }'

Response:

{
  "category": "settlement",
  "risk_level": "low",
  "purpose": "Business settlement for Q1 services",
  "involved_entities": [
    {
      "name": "Entity A",
      "role": "sender",
      "risk_factors": []
    },
    {
      "name": "Entity B",
      "role": "receiver",
      "risk_factors": []
    }
  ]
}

3. Market Analysis

Request:

curl -X POST https://api.tokensource.com/v1/structured-output \
  -H "Authorization: Bearer ts_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Analyze current market conditions for USDT: [Price: $1.001, 24h Volume: $45B, Major market events: Fed rate decision, Banking sector news]. Provide market sentiment and key factors affecting stability.",
    "schema": {
      "type": "object",
      "properties": {
        "market_sentiment": {
          "type": "string",
          "enum": ["bearish", "neutral", "bullish"]
        },
        "key_factors": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "factor": {"type": "string"},
              "impact": {"type": "string"},
              "confidence": {"type": "number", "minimum": 0, "maximum": 1}
            }
          }
        }
      }
    }
  }'

Response:

{
  "market_sentiment": "neutral",
  "key_factors": [
    {
      "factor": "Fed rate decision",
      "impact": "Potential decrease in demand for risky assets",
      "confidence": 0.7
    },
    {
      "factor": "Banking sector news",
      "impact": "Increased volatility and uncertainty",
      "confidence": 0.8
    }
  ]
}

4. Smart Contract Security Analysis

Request:

curl -X POST https://api.tokensource.com/v1/structured-output \
  -H "Authorization: Bearer ts_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Review this smart contract code for security vulnerabilities and optimization opportunities: [Sample Contract Code Here]",
    "schema": {
      "type": "object",
      "properties": {
        "security_assessment": {
          "type": "object",
          "properties": {
            "vulnerabilities": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "severity": {"type": "string", "enum": ["low", "medium", "high", "critical"]},
                  "description": {"type": "string"},
                  "mitigation": {"type": "string"}
                }
              }
            }
          }
        }
      }
    }
  }'

Response:

{
  "security_assessment": {
    "vulnerabilities": [
      {
        "severity": "high",
        "description": "Potential reentrancy vulnerability in withdrawal function",
        "mitigation": "Implement checks-effects-interactions pattern and use ReentrancyGuard"
      },
      {
        "severity": "medium",
        "description": "Unbounded loop in token distribution",
        "mitigation": "Add upper bound to loop iterations and implement batch processing"
      }
    ]
  }
}

5. Risk Profile Assessment

Request:

curl -X POST https://api.tokensource.com/v1/structured-output \
  -H "Authorization: Bearer ts_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Assess the risk profile of this wallet (0x456...789) based on: Transaction history of last 30 days, interaction with flagged addresses, trading patterns, and compliance history.",
    "schema": {
      "type": "object",
      "properties": {
        "risk_profile": {
          "type": "object",
          "properties": {
            "overall_risk": {"type": "string", "enum": ["low", "medium", "high"]},
            "risk_factors": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "factor": {"type": "string"},
                  "severity": {"type": "string"},
                  "mitigation_steps": {"type": "array", "items": {"type": "string"}}
                }
              }
            }
          }
        }
      }
    }
  }'

Response:

{
  "risk_profile": {
    "overall_risk": "medium",
    "risk_factors": [
      {
        "factor": "Interaction with flagged address",
        "severity": "medium",
        "mitigation_steps": [
          "Review transaction history",
          "Implement enhanced monitoring",
          "Request source of funds documentation"
        ]
      },
      {
        "factor": "Unusual trading pattern",
        "severity": "low",
        "mitigation_steps": [
          "Monitor for pattern continuation",
          "Flag for periodic review"
        ]
      }
    ]
  }
}

Last updated