[PR #2848] langgraph: add structured output to create_react_agent #2958

Closed
opened 2026-02-20 17:47:51 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/2848

State: closed
Merged: Yes


class WeatherResponse(BaseModel):
    """Respond to the user with this"""

    temperature: float = Field(description="The temperature in fahrenheit")
    wind_direction: str = Field(
        description="The direction of the wind in abbreviated form"
    )
    wind_speed: float = Field(description="The speed of the wind in mph")

@tool
def get_weather(city: Literal["nyc", "sf"]):
    """Use this to get weather information."""
    if city == "nyc":
        return "It is cloudy in NYC, with 5 mph winds in the North-East direction and a temperature of 70 degrees"
    elif city == "sf":
        return "It is 75 degrees and sunny in SF, with 3 mph winds in the South-East direction"
    else:
        raise AssertionError("Unknown city")

model = ChatOpenAI()
tools = [get_weather]
agent_with_structured_output = create_react_agent(model, tools, response_format=WeatherResponse)
agent_with_structured_output.invoke({"messages": [("user", "what's the weather in nyc?")]})
{
    'messages': [...],
    'structured_response': WeatherResponse(temperature=70.0, wind_directon='NE', wind_speed=5.0)
}
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/2848 **State:** closed **Merged:** Yes --- ```python class WeatherResponse(BaseModel): """Respond to the user with this""" temperature: float = Field(description="The temperature in fahrenheit") wind_direction: str = Field( description="The direction of the wind in abbreviated form" ) wind_speed: float = Field(description="The speed of the wind in mph") @tool def get_weather(city: Literal["nyc", "sf"]): """Use this to get weather information.""" if city == "nyc": return "It is cloudy in NYC, with 5 mph winds in the North-East direction and a temperature of 70 degrees" elif city == "sf": return "It is 75 degrees and sunny in SF, with 3 mph winds in the South-East direction" else: raise AssertionError("Unknown city") model = ChatOpenAI() tools = [get_weather] agent_with_structured_output = create_react_agent(model, tools, response_format=WeatherResponse) agent_with_structured_output.invoke({"messages": [("user", "what's the weather in nyc?")]}) ``` ```pycon { 'messages': [...], 'structured_response': WeatherResponse(temperature=70.0, wind_directon='NE', wind_speed=5.0) } ```
yindo added the pull-request label 2026-02-20 17:47:51 -05:00
yindo closed this issue 2026-02-20 17:47:51 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#2958