How to get structured output out of a ReAct Agent #25359
Replies: 2 comments 5 replies
-
Hello, @invalidexplorer! I'm here to assist you with any bugs, questions, or contributions you might have. Let's work together to resolve your issue. To get structured output from a ReAct Agent in LangChain without encountering JSON parsing errors, you can use the Here is an example of how you can use it: import re
from langchain_core.language_models import BaseLanguageModel
from langchain.agents.output_parsers.react import ReActOutputParser
from langchain_core.exceptions import OutputParserException
# Assuming you have an instance of a language model
llm = BaseLanguageModel()
# Create the parser
parser = ReActOutputParser()
# Example text to parse
text = """
Action: Final Answer[The result is 42]
"""
# Parse the text
try:
result = parser.parse(text)
print(result)
except OutputParserException as e:
print(f"Error parsing output: {e}") This approach ensures that the output is parsed correctly according to the expected format, reducing the likelihood of JSON parsing errors. The |
Beta Was this translation helpful? Give feedback.
-
Hello, @invalidexplorer! I'm here to assist you with any bugs, questions, or contributions you might have. Let's work together to resolve your issue. To get structured output from a ReAct Agent in LangChain without encountering JSON parsing errors, you can use the Here is an example of how you can use it: import re
from langchain_core.language_models import BaseLanguageModel
from langchain.agents.output_parsers.react import ReActOutputParser
from langchain_core.exceptions import OutputParserException
# Assuming you have an instance of a language model
llm = BaseLanguageModel()
# Create the parser
parser = ReActOutputParser()
# Example text to parse
text = """
Action: Final Answer[The result is 42]
"""
# Parse the text
try:
result = parser.parse(text)
print(result)
except OutputParserException as e:
print(f"Error parsing output: {e}") This approach ensures that the output is parsed correctly according to the expected format, reducing the likelihood of JSON parsing errors. The |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I have tried to use ReAct agents and format their output using "from langchain.output_parsers import ResponseSchema,StructuredOutputParser
code=ResponseSchema(name="code",description="well formatted python code")
description=ResponseSchema(name="description",description="approach of how the problem was solved, any errors or blockers if any faced")
output_parser=StructuredOutputParser.from_response_schemas([code,description])" however the react agents were stuck on a loop "Got invalid JSON object. Error: Expecting value: line 1 column 1 (char 0)Invalid or incomplete responseGot invalid JSON object. Error: Expecting value: line 1 column 1 (char 0)Invalid or incomplete responseGot invalid JSON object. Error: Expecting value: line 1 column 1 (char 0)Invalid or incomplete response
". Would really appreciate on how to go about solving them.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
Beta Was this translation helpful? Give feedback.
All reactions