Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: Filter out blank text returned by the LLM when calling tools

kdgmpw6d62d4851ca8
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index 53c8718197..ee620ad70b 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -4494,6 +4494,8 @@ class JiuWenClawDeepAdapter:
if isinstance(chunk.payload, dict)
else str(chunk.payload)
)
+ if not content or not content.strip():
+ continue
yield AgentResponseChunk(
request_id=rid,
channel_id=cid,
@@ -4503,6 +4505,13 @@ class JiuWenClawDeepAdapter:
continue
if chunk_type == "llm_output":
+ content = (
+ chunk.payload.get("content", "")
+ if isinstance(chunk.payload, dict)
+ else str(chunk.payload)
+ )
+ if not content or not content.strip():
+ continue
has_streamed_content = True
if accumulated_reasoning:
yield AgentResponseChunk(
@@ -4515,11 +4524,6 @@ class JiuWenClawDeepAdapter:
is_complete=False,
)
accumulated_reasoning = ""
- content = (
- chunk.payload.get("content", "")
- if isinstance(chunk.payload, dict)
- else str(chunk.payload)
- )
yield AgentResponseChunk(
request_id=rid,
channel_id=cid,
@@ -4711,7 +4715,7 @@ class JiuWenClawDeepAdapter:
content = (
payload.get("content", "") if isinstance(payload, dict) else str(payload)
)
- if not content:
+ if not content or not content.strip():
return None
return {"event_type": "chat.delta", "content": content}
@@ -4721,7 +4725,7 @@ class JiuWenClawDeepAdapter:
if isinstance(payload, dict)
else str(payload)
)
- if not content:
+ if not content or not content.strip():
return None
return {"event_type": "chat.reasoning", "content": content}
@@ -4729,7 +4733,7 @@ class JiuWenClawDeepAdapter:
content = (
payload.get("content", "") if isinstance(payload, dict) else str(payload)
)
- if not content:
+ if not content or not content.strip():
return None
return {"event_type": "chat.delta", "content": content}
@@ -4751,11 +4755,11 @@ class JiuWenClawDeepAdapter:
content = str(payload)
is_chunked = False
+ if not content or not content.strip():
+ return None
+
if _has_streamed_content and not is_chunked:
return {"event_type": "chat.final", "content": content}
-
- if not content:
- return None
if is_chunked:
return {"event_type": "chat.delta", "content": content}
return {"event_type": "chat.final", "content": content}
@@ -4961,10 +4965,10 @@ class JiuWenClawDeepAdapter:
if "traceId" in payload or "invokeId" in payload:
return None
content = payload.get("content") or payload.get("output")
- if not content:
- return None
else:
content = str(payload)
+ if not content or not content.strip():
+ return None
return {"event_type": "chat.delta", "content": content}
if isinstance(chunk, dict):
diff --git a/jiuwenswarm/server/utils/stream_utils.py b/jiuwenswarm/server/utils/stream_utils.py
index 437242437b..64116471e4 100644
--- a/jiuwenswarm/server/utils/stream_utils.py
+++ b/jiuwenswarm/server/utils/stream_utils.py
@@ -69,9 +69,12 @@ def _parse_dict_chunk(chunk: dict[str, Any], _has_streamed_content: bool) -> dic
}
if "content" in chunk:
+ content = chunk.get("content", "")
+ if not content or not content.strip():
+ return None
return {
"event_type": "chat.delta" if not _has_streamed_content else "chat.final",
- "content": chunk.get("content", ""),
+ "content": content,
}
if "output" in chunk:
@@ -81,9 +84,12 @@ def _parse_dict_chunk(chunk: dict[str, Any], _has_streamed_content: bool) -> dic
"event_type": "chat.error",
"error": chunk.get("output", ""),
}
+ output = chunk.get("output", "")
+ if not output or not output.strip():
+ return None
return {
"event_type": "chat.delta" if not _has_streamed_content else "chat.final",
- "content": chunk.get("output", ""),
+ "content": output,
}
return chunk
@@ -132,7 +138,7 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
if isinstance(payload, dict)
else str(payload)
)
- if not content:
+ if not content or not content.strip():
return None
return {"event_type": "chat.delta", "content": content}
@@ -142,7 +148,7 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
if isinstance(payload, dict)
else str(payload)
)
- if not content:
+ if not content or not content.strip():
return None
return {"event_type": "chat.reasoning", "content": content}
@@ -152,7 +158,7 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
if isinstance(payload, dict)
else str(payload)
)
- if not content:
+ if not content or not content.strip():
return None
return {"event_type": "chat.delta", "content": content}
@@ -178,11 +184,11 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
content = str(payload)
is_chunked = False
+ if not content or not content.strip():
+ return None
+
if _has_streamed_content and not is_chunked:
return {"event_type": "chat.final", "content": content}
-
- if not content:
- return None
if is_chunked:
return {"event_type": "chat.delta", "content": content}
return {"event_type": "chat.final", "content": content}