fix(stream): non-stream fallback now yields serialized response end; prevents ModelResponse JSON error and preserves final event

This commit is contained in:
Vukasin 2025-08-08 09:56:54 +02:00
parent 1144fea84a
commit a948e63c4d
1 changed files with 16 additions and 2 deletions

View File

@ -1009,11 +1009,25 @@ class ResponseProcessor:
# --- Save and Yield assistant_response_end ---
if assistant_message_object: # Only save if assistant message was saved
try:
# Save the full LiteLLM response object directly in content
# Ensure content is JSON-serializable
serializable_response = None
try:
if hasattr(llm_response, "model_dump_json"):
import json as _json
serializable_response = _json.loads(llm_response.model_dump_json())
elif hasattr(llm_response, "model_dump"):
serializable_response = llm_response.model_dump() # type: ignore[attr-defined]
elif hasattr(llm_response, "dict"):
serializable_response = llm_response.dict() # type: ignore[attr-defined]
else:
serializable_response = str(llm_response)
except Exception:
serializable_response = str(llm_response)
await self.add_message(
thread_id=thread_id,
type="assistant_response_end",
content=llm_response,
content=serializable_response,
is_llm_message=False,
metadata={"thread_run_id": thread_run_id}
)