From 7b03355885cafba4d0c463b324fbb4ee6cf828a4 Mon Sep 17 00:00:00 2001 From: Yip Rui Fung Date: Wed, 15 Apr 2026 08:34:24 +0800 Subject: [PATCH] fix: handle multiple begin/end blocks in n8n streaming Remove premature break on 'end' chunk type to support multi-step n8n agent workflows and add error chunk handling. Co-authored-by: opencode --- custom_components/webhook_conversation/entity.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/custom_components/webhook_conversation/entity.py b/custom_components/webhook_conversation/entity.py index ad68dd4..d268ac9 100644 --- a/custom_components/webhook_conversation/entity.py +++ b/custom_components/webhook_conversation/entity.py @@ -152,13 +152,16 @@ async def _send_payload_streaming( if line_str: try: chunk_data = json.loads(line_str) - if ( - chunk_data.get("type") == "item" - and "content" in chunk_data - ): + chunk_type = chunk_data.get("type") + if chunk_type == "item" and "content" in chunk_data: yield chunk_data["content"] - elif chunk_data.get("type") == "end": - break + elif chunk_type == "error": + raise HomeAssistantError( + f"n8n error: {chunk_data.get('message', chunk_data)}" + ) + # We don't break on "end" because n8n can send multiple + # begin/end blocks when using tools or intermediate steps. + # We keep reading until the stream actually closes. except json.JSONDecodeError: _LOGGER.warning( "Failed to parse streaming response chunk: %s", line_str