Structured Output Response

Structured output is a feature that allows large language models (LLMs) to generate responses in a specific format with your resources. Structured output is best for extracting structured data to fit your application’s needs. It returns data formatted with the json_schema and json_schema_rules you provide. To use the structured output feature you need to provide the following parameters:

Streaming is not supported when doing structured output response

ParameterTypeDescription
collection_idStringThe unique ID of the collection to query obtained from the /collection/create/ endpoint.
request_queryStringThe search term or query text.
json_schemaStringThe desired structure for the JSON response.
json_schema_rulesStringCustom rules to guide the response’s adherence to the specified JSON schema.
model(optional)StringSpecific model you desire to query response Check out our list of supported models here

json_schema

The json_schema parameter defines the structure of the response you want the system to return. This parameter helps ensure the output matches your application’s requirements.

Example JSON Schema

{
    "topic" : "string",
    "description" : "string",
    "count" : "int"
}

Example JSON Schema (List)

If you want a list of items and not just a single item, you can specify the json schema by putting the entire object in a list

[
  {
    "topic" : "string",
    "description" : "string",
    "count" : "int"
  }
]

json_schema_rules

The json_schema_rules parameter allows you to define custom rules for how the schema is applied. Another way to think about this is that it is a set of instructions for the LLM to follow when generating the response. This can also be in form of a more detailed prompt This can include constraints on the data, additional formatting, or validation logic.

Example JSON Schema Rules

rules = "Give a very short description of every page on the document from"

Example JSON Schema Rules (List)

rules = ["Give a very short description of every page on the document from", "Make sure there are no less than 5 topics"]

Example Requests

from wetro import Wetrocloud

# Initialize the Wetrocloud client
client = Wetrocloud(api_key="your_api_key")

# Define a JSON schema for structured output
json_schema = [{"month": "", "sales": "int"}]

# Add processing rules
rules = "Give a breakdown of the sales for each month"

# Query with structured output requirements
response = client.collection.query_collection(
    collection_id="my_unique_collection_id",
    request_query="Generate a sales report",
    json_schema=json_schema,
    json_schema_rules=rules
)
print(response)

Example Response

{
    "response": [
        {
            "month": "January",
            "sales": 15000
        },
        {
            "month": "February",
            "sales": 18000
        },
        {
            "month": "March",
            "sales": 17000
        }
    ],
    "tokens": 4007,
    "success": true
}
FieldDescription
responseJSON object structured as per the provided schema.
tokensNumber of tokens used for processing.
successIndicates whether the query was successful.