YouTip LogoYouTip

Langchain Structured Output Strategies

LangChain Output Strategies | Beginner Tutorial

LangChain Output Strategies | Beginner Tutorial

LangChain provides three structured output strategies. Understanding their differences and how they work can help you make the best choice in different scenarios.


Overview of Three Strategies

Strategy Principle Model Support Response Speed
ToolStrategy Disguises Schema as a tool, the model "calls" this tool to output structured data All models that support function calling Slower (one more tool call)
ProviderStrategy Uses the model's native structured output capability (like OpenAI's response_format) Some models (GPT-4o+, Claude 3+, etc.) Faster (direct output)
AutoStrategy Automatically detects model capabilities and selects the best strategy Auto-adapt Automatically selects optimal

ToolStrategy - Tool Calling Mode

ToolStrategy is the most compatible method. It converts your Schema into a "fake tool", and the model outputs structured data by calling this tool.

Example

from pydantic import BaseModel, Field

from langchain.agents import create_agent

from langchain.agents.structured_output import ToolStrategy

from langchain.chat_models import init_chat_model

from langchain.messages import HumanMessage

class WeatherReport(BaseModel):

    """Weather report"""

    city: str = Field(description="City name")

    temperature: float = Field(description="Temperature (Celsius)")

    condition: str = Field(description="Weather condition")

    humidity: int = Field(description="Humidity percentage")

model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)

# Explicitly specify using ToolStrategy

agent = create_agent(

    model=model,

    response_format=ToolStrategy(schema=WeatherReport),

    system_prompt="You are a weather assistant, generate structured weather reports based on user descriptions.",

)

result = agent.invoke({

    "messages": [HumanMessage(content="Hangzhou is sunny today, temperature 25Β°C, humidity 60%")]

})

report = result

print(f"City: {report.city}")

print(f"Temperature: {report.temperature}Β°C")

print(f"Condition: {report.condition}")

print(f"Humidity: {report.humidity}%")

# View execution process - you can see one more tool call message

print(f"\nMessage count: {len(result['messages'])}")

for msg in result:

    print(f"  [{msg.type}]", end="")

    if hasattr(msg, 'tool_calls') and msg.tool_calls:

        print(f"  Call: {[tc['name'] for tc in msg.tool_calls]}")

    elif msg.type == "tool":

        print(f"  {msg.content[:60]}")

    else:

        print(f"  {str(msg.content)[:60]}")

Running result:

City: Hangzhou
Temperature: 25.0Β°C
Condition: sunny
Humidity: 60%
Message count: 4
  Hangzhou is sunny today, temperature 25Β°C, humidity 60%
  Call: ['WeatherReport']
  Returning structured response: ...

As you can see, ToolStrategy adds one more tool call step (calling the "fake tool" named WeatherReport), and then outputs the structured data.

handle_errors - Error Retry

ToolStrategy supports automatic retry when structured output fails:

Example

from langchain.agents.structured_output import ToolStrategy

# handle_errors=True: When output format is wrong, feedback the error message to the model for retry

strategy_with_retry = ToolStrategy(

    schema=WeatherReport,

    handle_errors=True,  # Default False

)

# handle_errors can also be a custom error message template

strategy_custom_error = ToolStrategy(

    schema=WeatherReport,

    handle_errors="Format is incorrect, please correct and re-output according to {error}",

)

ProviderStrategy - Native Structured Output

ProviderStrategy uses the model provider's native capability (like OpenAI's response_format parameter). Not all models support this.

Example

from pydantic import BaseModel, Field

from langchain.agents import create_agent

from langchain.agents.structured_output import ProviderStrategy

from langchain.chat_models import init_chat_model

from langchain.messages import HumanMessage

class CourseInfo(BaseModel):

    """Course information"""

    name: str = Field(description="Course name")

    level: str = Field(description="Difficulty: Beginner/Intermediate/Advanced")

    price: str = Field(description="Price information")

model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)

# Explicitly specify ProviderStrategy

agent = create_agent(

    model=model,

    response_format=ProviderStrategy(schema=CourseInfo),

    system_prompt="You are the course assistant of Beginner Tutorial TUTORIAL.",

)

result = agent.invoke({

    "messages": [HumanMessage(content="Python3 Basic Tutorial is a beginner-level free course")]

})

course = result

print(f"Course: {course.name}")

print(f"Difficulty: {course.level}")

print(f"Price: {course.price}")

print(f"\nMessage count: {len(result['messages'])}")  # Less than ToolStrategy

Running result:

Course: Python3 Basic Tutorial
Difficulty: Beginner
Price: Free
Message count: 2

Compared to ToolStrategy, ProviderStrategy has fewer messages (2 vs 4) because it doesn't need the additional tool call step.

ProviderStrategy is currently mainly supported by OpenAI's GPT-4o and above, and Claude 3 and above. If the model doesn't support it, LangChain will automatically fall back to ToolStrategy. You can use model.profile to check if the model supports it.

← Langchain Before After ModelLangchain Streaming β†’