Skip to content

Structured Output fail with Claude Sonnet and DeepSeek providers #2032

@sebastianherreramonterrosa

Description

Structured Output have problems parsing the answer when the provider is Claude and Deepseek. It is working with models of openai, google, xai.

Example code

from agents import Agent, OpenAIChatCompletionsModel, Runner, set_tracing_disabled
from openai import AsyncOpenAI
from pydantic import BaseModel


set_tracing_disabled(disabled=True)

## Claude Provider
claude_api_key = "..."
provider = AsyncOpenAI(api_key=claude_api_key, base_url="https://api.anthropic.com/v1/")
model = OpenAIChatCompletionsModel(model="claude-sonnet-4-5", openai_client=provider)

## Deepseek Provider
deepseek_api_key = "..."
provider = AsyncOpenAI(api_key=deepseek_api_key, base_url="https://api.deepseek.com")
model = OpenAIChatCompletionsModel(model="deepseek-reasoner", openai_client=provider)

class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]


agent = Agent(
    name="Calendar extractor",
    instructions="Extract calendar events from text and return them in structured JSON format.",
    output_type=CalendarEvent,
    model=model,
)


input_text = """
Let's schedule a team meeting called 'Product Kickoff' on October 15th, 2025.
Participants will be Alice, Bob, and Charlie.
"""


result = Runner.run_sync(agent, input_text)
print(result.final_output)


result.final_output.model_dump()

Error

Claude Error:

ValidationError: 1 validation error for CalendarEvent
  Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='```json\n{\n  "events": ...  ]\n    }\n  ]\n}\n```', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/json_invalid
...
  ]
}
``` for TypeAdapter(CalendarEvent); 1 validation error for CalendarEvent
  Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='```json\n{\n  "events": ...  ]\n    }\n  ]\n}\n```', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/json_invalid

Deepseek Error:

---------------------------------------------------------------------------
BadRequestError                           Traceback (most recent call last)
Cell In[14], [line 1](vscode-notebook-cell:?execution_count=14&line=1)
----> [1](vscode-notebook-cell:?execution_count=14&line=1) result = Runner.run_sync(agent, input_text)
      2 print(result.final_output)

File c:\Users\sejohermo\AppData\Local\Programs\Python\Python312\Lib\site-packages\agents\run.py:324, in Runner.run_sync(cls, starting_agent, input, context, max_turns, hooks, run_config, previous_response_id, conversation_id, session)
    293 """Run a workflow synchronously, starting at the given agent. Note that this just wraps the
    294 `run` method, so it will not work if there's already an event loop (e.g. inside an async
    295 function, or in a Jupyter notebook or async context like FastAPI). For those cases, use
   (...)
    321     agent. Agents may perform handoffs, so we don't know the specific type of the output.
    322 """
    323 runner = DEFAULT_AGENT_RUNNER
--> [324](file:///C:/Users/sejohermo/AppData/Local/Programs/Python/Python312/Lib/site-packages/agents/run.py:324) return runner.run_sync(
    325     starting_agent,
    326     input,
    327     context=context,
    328     max_turns=max_turns,
    329     hooks=hooks,
    330     run_config=run_config,
    331     previous_response_id=previous_response_id,
    332     conversation_id=conversation_id,
    333     session=session,
    334 )
...
-> [1594](file:///C:/Users/sejohermo/AppData/Local/Programs/Python/Python312/Lib/site-packages/openai/_base_client.py:1594)         raise self._make_status_error_from_response(err.response) from None
   1596     break
   1598 assert response is not None, "could not resolve response (should never happen)"

BadRequestError: Error code: 400 - {'error': {'message': 'This response_format type is unavailable now', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

Debug information

  • Agents SDK version: 0.2.11
  • Python version: 3.12.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions