Last active
July 4, 2025 10:13
-
-
Save vbarda/26f97ed2020e4a3f55e2ee2aae4abc5a to your computer and use it in GitHub Desktop.
react_agent_structured_output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel, Field | |
from typing import Literal | |
from langchain_core.tools import tool | |
from langchain_anthropic import ChatAnthropic | |
from langgraph.graph import MessagesState | |
class WeatherResponse(BaseModel): | |
"""Respond to the user with this""" | |
temperature: float = Field(description="The temperature in fahrenheit") | |
wind_directon: str = Field( | |
description="The direction of the wind in abbreviated form" | |
) | |
wind_speed: float = Field(description="The speed of the wind in km/h") | |
@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") | |
@tool(return_direct=True, response_format="content_and_artifact", args_schema=WeatherResponse) | |
def get_structured_response(**tool_args): | |
# this will put tool args into the ToolMessage.artifact. | |
# also, since the tool is marked as return_direct=True, create_react_agent will exit when it calls this tool | |
return "Here is your structured response", tool_args | |
tools = [get_weather, get_structured_response] | |
model = ChatAnthropic(model="claude-3-7-sonnet-latest") | |
agent = create_react_agent(model, tools) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment