What is structured output?

Structured Outputs is a feature that ensures the model will always generate responses that adhere to your supplied JSON Schema, so you don’t need to worry about the model omitting a required key, or hallucinating an invalid enum value.

How to use structured output with Anthropic models

We currently only support structured output with Anthropic models only when you use OpenAI SDK. Which means to get structured output from Anthropic models, you should call anthropic models using the OpenAI SDK client to our endpoint.

Example code

You can simply add response_format={json_schema: {YOUR_JSON_SCHEMA}} to your request. Under the hood, we create a function that asks Anthropic model to response in JSON format.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.keywordsai.co/api/",
    api_key="YOUR_KEYWORDSAI_API_KEY",
)

response = client.chat.completions.create(
    model="claude-3-5-sonnet-20240620", # any anthropic model
    messages=[
        {"role": "user", "content": "Tell me a long story"}
    ],
    response_format={json_schema: {YOUR_JSON_SCHEMA}}
)