From 2fa6d868a1e8046d6de6bd4c0399d2dced3386b6 Mon Sep 17 00:00:00 2001 From: Edwin Jose Date: Fri, 2 May 2025 10:49:44 -0400 Subject: [PATCH] fix: Streaming for anthropic models in Agents (#7871) * fix streaming for anthropic models and optimise the _extract_output_text * Update src/backend/base/langflow/base/agents/events.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: improve handling of AIMessageChunk state and output text extraction * fix: change ValueError to TypeError in output text extraction * fix: ensure output text is appended only if it is valid and agent message text is a string --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida --- .../base/langflow/base/agents/events.py | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/backend/base/langflow/base/agents/events.py b/src/backend/base/langflow/base/agents/events.py index c9dbc75fc..a3846af8a 100644 --- a/src/backend/base/langflow/base/agents/events.py +++ b/src/backend/base/langflow/base/agents/events.py @@ -81,13 +81,25 @@ async def handle_on_chain_start( def _extract_output_text(output: str | list) -> str: if isinstance(output, str): - text = output - elif isinstance(output, list) and len(output) == 1 and isinstance(output[0], dict) and "text" in output[0]: - text = output[0]["text"] - else: + return output + if isinstance(output, list) and len(output) == 0: + return "" + if not isinstance(output, list) or len(output) != 1: msg = f"Output is not a string or list of dictionaries with 'text' key: {output}" - raise ValueError(msg) - return text + raise TypeError(msg) + + item = output[0] + if isinstance(item, str): + return item + if isinstance(item, dict): + if "text" in item: + return item["text"] + # If the item's type is "tool_use", return an empty string. + # This likely indicates that "tool_use" outputs are not meant to be displayed as text. + if item.get("type") == "tool_use": + return "" + msg = f"Output is not a string or list of dictionaries with 'text' key: {output}" + raise TypeError(msg) async def handle_on_chain_end( @@ -214,9 +226,11 @@ async def handle_on_chain_stream( agent_message = await send_message_method(message=agent_message) start_time = perf_counter() elif isinstance(data_chunk, AIMessageChunk): - agent_message.text += data_chunk.content - agent_message.properties.state = "complete" - agent_message = await send_message_method(message=agent_message) + output_text = _extract_output_text(data_chunk.content) + if output_text and isinstance(agent_message.text, str): + agent_message.text += output_text + agent_message.properties.state = "partial" + agent_message = await send_message_method(message=agent_message) start_time = perf_counter() return agent_message, start_time