[PR #2674] Allow the structured output resonse with create_react_agent #2851

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

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

State: closed
Merged: No


Added response_format in create_react_agent method to allow the chat model to return structured output in JSON string based on pydantic BaseModel.

If response_format is provided, the AIMessaged returned chat model would have a JSON string in its content.

Example of structured output -

#example.py
from typing import Optional, TypedDict, Any, Type
from pydantic import BaseModel, Field

import json

from langgraph.prebuilt import create_react_agent, ToolNode
from langchain_core.tools import BaseTool
from langchain_openai import ChatOpenAI


class ResponseSchema(BaseModel):
    is_related_query: bool = Field(description='Indicates if user query was related to the domestic pets')
    response: str = Field(description='content response of the chat assistant')


class UpdateFavoritePetTool(BaseTool):
    name: str = 'UpdateFavoritePetTool'
    description: str = 'Updates the favorite pet of the tool for current user'

    class InputSchema(BaseModel):
        pet_name: str = Field(description='Name of the pet')

    args_schema: Type[BaseModel] = InputSchema

    def _run(self, pet_name: str, **kwargs):
        print('updating the favorite pet of the user to', pet_name)
        return {'success': True}

my_tools = [UpdateFavoritePetTool()]

system_prompt = """
You are a helpful assistant specializing in the field of domestic pets.
You have some tools available for certain actions.
YOU SHOULD NOT ANSWER ANTHING ELSE OTHER THAN QUERIES RELATED TO DOMESTIC PETS.

Your response format should strictly in JSON format.
"""

open_ai_model = ChatOpenAI(model="gpt-4o-2024-08-06", openai_api_key=OPEN_AI_KEY)
assistant = create_react_agent(
            open_ai_model,
            my_tools,
            state_modifier=system_prompt,
            response_format=ResponseSchema
          )

user_input = input("Enter your query:\n")    
assistant_response = assistant.invoke({"messages": [("user", user_input)]})
messages = assistant_response.get('messages')
parsed_ai_response = json.loads(messages[-1].content)
print('\nAI response: ',parsed_ai_response)
print('is query related to domestic pets', parsed_ai_response['is_related_query'])

---------------- Run the file ----------------

$ python example.py
Enter your query:
How's the weather

AI response: {'is_related_query': False, 'response': "I'm here to assist with questions about domestic pets."}
is query related to domestic pets: False

---------------- Run the file again ----------------

$ python example.py
Enter your query:
Update my favorite pet to Labrador dogs

AI response: {'is_related_query': True, 'response': 'Your favorite pet has been updated to Labrador dogs.'}
is query related to domestic pets: True

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/2674 **State:** closed **Merged:** No --- Added `response_format` in `create_react_agent` method to allow the chat model to return structured output in JSON string based on pydantic BaseModel. If `response_format` is provided, the AIMessaged returned chat model would have a JSON string in its `content`. ### Example of structured output - ``` #example.py from typing import Optional, TypedDict, Any, Type from pydantic import BaseModel, Field import json from langgraph.prebuilt import create_react_agent, ToolNode from langchain_core.tools import BaseTool from langchain_openai import ChatOpenAI class ResponseSchema(BaseModel): is_related_query: bool = Field(description='Indicates if user query was related to the domestic pets') response: str = Field(description='content response of the chat assistant') class UpdateFavoritePetTool(BaseTool): name: str = 'UpdateFavoritePetTool' description: str = 'Updates the favorite pet of the tool for current user' class InputSchema(BaseModel): pet_name: str = Field(description='Name of the pet') args_schema: Type[BaseModel] = InputSchema def _run(self, pet_name: str, **kwargs): print('updating the favorite pet of the user to', pet_name) return {'success': True} my_tools = [UpdateFavoritePetTool()] system_prompt = """ You are a helpful assistant specializing in the field of domestic pets. You have some tools available for certain actions. YOU SHOULD NOT ANSWER ANTHING ELSE OTHER THAN QUERIES RELATED TO DOMESTIC PETS. Your response format should strictly in JSON format. """ open_ai_model = ChatOpenAI(model="gpt-4o-2024-08-06", openai_api_key=OPEN_AI_KEY) assistant = create_react_agent( open_ai_model, my_tools, state_modifier=system_prompt, response_format=ResponseSchema ) user_input = input("Enter your query:\n") assistant_response = assistant.invoke({"messages": [("user", user_input)]}) messages = assistant_response.get('messages') parsed_ai_response = json.loads(messages[-1].content) print('\nAI response: ',parsed_ai_response) print('is query related to domestic pets', parsed_ai_response['is_related_query']) ``` ### ---------------- Run the file ---------------- ``` $ python example.py Enter your query: How's the weather AI response: {'is_related_query': False, 'response': "I'm here to assist with questions about domestic pets."} is query related to domestic pets: False ``` ### ---------------- Run the file again ---------------- ``` $ python example.py Enter your query: Update my favorite pet to Labrador dogs AI response: {'is_related_query': True, 'response': 'Your favorite pet has been updated to Labrador dogs.'} is query related to domestic pets: True ```
yindo added the pull-request label 2026-02-20 17:47:41 -05:00
yindo closed this issue 2026-02-20 17:47:41 -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#2851