fix: streaming error when only_need_context=True returns empty results

Prevents NoneType async iteration error by handling None responses
in stream_generator and ensuring kg_query returns valid strings.
This commit is contained in:
yangdx 2025-06-28 09:18:06 +08:00
parent a506753548
commit 0f51ec48f1
2 changed files with 4 additions and 1 deletions

View File

@ -183,6 +183,9 @@ def create_query_routes(rag, api_key: Optional[str] = None, top_k: int = 60):
if isinstance(response, str):
# If it's a string, send it all at once
yield f"{json.dumps({'response': response})}\n"
elif response is None:
# Handle None response (e.g., when only_need_context=True but no context found)
yield f"{json.dumps({'response': 'No relevant context found for the query.'})}\n"
else:
# If it's an async generator, send chunks one by one
try:

View File

@ -1390,7 +1390,7 @@ async def kg_query(
)
if query_param.only_need_context:
return context
return context if context is not None else PROMPTS["fail_response"]
if context is None:
return PROMPTS["fail_response"]