Answer chunks not coming in with JsonOutputParser #24966
Replies: 2 comments
-
Hey there, @PjCnuddelement! I'm a bot here to help you with bugs, questions, and more while you wait for a human maintainer. If you need assistance, feel free to ask! To resolve the issue where the chunk following the context chunk is STREAMED_MESSAGES_WITH_TOOL_CALLS = []
for message in STREAMED_MESSAGES:
if message.additional_kwargs:
STREAMED_MESSAGES_WITH_TOOL_CALLS.append(
AIMessageChunk(
content=message.content,
additional_kwargs=message.additional_kwargs,
tool_call_chunks=[
ToolCallChunk(
name=chunk["function"].get("name"),
args=chunk["function"].get("arguments"),
id=chunk.get("id"),
index=chunk["index"],
)
for chunk in message.additional_kwargs["tool_calls"]
],
)
)
else:
STREAMED_MESSAGES_WITH_TOOL_CALLS.append(message) Additionally, ensure that your class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]):
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
text = result[0].text
text = text.strip()
if partial:
try:
return parse_json_markdown(text)
except JSONDecodeError:
return None
else:
try:
return parse_json_markdown(text)
except JSONDecodeError as e:
msg = f"Invalid json output: {text}"
raise OutputParserException(msg, llm_output=text) from e Ensure that your For further robustness, refer to the test cases provided in the LangChain library, which include various scenarios for parsing JSON, including partial JSON objects and JSON within code blocks [1][2][3]. |
Beta Was this translation helpful? Give feedback.
-
@dosu this answer is so wrong! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Before JsonOutputParser I was using the StrOutputParser and I could see all the chunks coming in for context, input and answer. However, since I changed to the JsonOutputParser, only the input and context chunk are coming in. The chunk following the context chunk is None.
System Info
System Information
Package Information
Beta Was this translation helpful? Give feedback.
All reactions