Skip to content

json_output_parser

Parse free-text JSON from models that reject response_format (e.g. DeepSeek thinking mode).

The problem

When you use create_agent(response_format=...), the model returns validated Pydantic models. But some models, such as DeepSeek in reasoning mode, reject all forms of response_format. They output free-text JSON in the message content instead, often with reasoning noise before the actual output.

How this bite helps

extract_structured_from_messages scans the last AI message for JSON, repairs malformed or truncated JSON, and validates it against your Pydantic schema. It handles reasoning noise, markdown code fences, and token-limit truncation.

What topologies it supports

  • create_agent with response_format, where the model may reject it.
  • Any agent that needs structured output from a model that will not honor response_format.
  • DeepSeek thinking or reasoning mode, where the model embeds JSON in its message content.

Example

See examples/json_output_parser.py for a runnable example of this bite. Run it with:

uv run python examples/json_output_parser.py

API reference

langshark_bites.json_output_parser

Shared utility to extract structured output from agent messages.

When create_agent(response_format=...) is used, the model returns validated Pydantic models via state["structured_response"]. However, DeepSeek's reasoning/thinking mode rejects all forms of response_format (tool_choice, json_schema, json_object). In that case the model outputs free-text JSON in its message content, and this utility parses it.

Usage::

from langshark_bites.json_output_parser import (
    extract_structured_from_messages,
)

content = state.get("structured_response")
if content is None:
    content = extract_structured_from_messages(
        state.get("messages", []), MySchema
    )

extract_structured_from_messages

extract_structured_from_messages(messages, schema_cls)

Parse the last AI message's content as JSON and validate against schema_cls.

Parameters:

Name Type Description Default

messages

list[dict[str, Any]]

The agent's message list from state["messages"].

required

schema_cls

type[BaseModel]

A Pydantic model class to validate against.

required

Returns:

Type Description
BaseModel | None

An instance of schema_cls if parsing and validation succeed,

BaseModel | None

None otherwise.