Handle tool call errors during streaming response serialization

- Add try-catch around chunk serialization in stream_generator functions
- Skip chunks that fail to serialize (e.g., tool calls without tool_choice)
- Log warnings for chunk serialization errors
- Prevent streaming failures when models attempt tool calls without proper configuration
- Apply fix to both RequestHandler and AutoselectHandler streaming methods
parent 08361f16
...@@ -121,10 +121,17 @@ class RequestHandler: ...@@ -121,10 +121,17 @@ class RequestHandler:
stream=True stream=True
) )
for chunk in response: for chunk in response:
# Convert chunk to dict and serialize as JSON try:
chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk # Convert chunk to dict and serialize as JSON
import json chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk
yield f"data: {json.dumps(chunk_dict)}\n\n".encode('utf-8') import json
yield f"data: {json.dumps(chunk_dict)}\n\n".encode('utf-8')
except Exception as chunk_error:
# Handle errors during chunk serialization (e.g., tool calls without tool_choice)
import logging
logging.warning(f"Error serializing chunk: {str(chunk_error)}")
# Skip this chunk and continue with the next one
continue
handler.record_success() handler.record_success()
except Exception as e: except Exception as e:
handler.record_failure() handler.record_failure()
...@@ -650,10 +657,16 @@ class AutoselectHandler: ...@@ -650,10 +657,16 @@ class AutoselectHandler:
{**request_data, "stream": True} {**request_data, "stream": True}
) )
for chunk in response: for chunk in response:
# Convert chunk to dict and serialize as JSON try:
chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk # Convert chunk to dict and serialize as JSON
import json chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk
yield f"data: {json.dumps(chunk_dict)}\n\n".encode('utf-8') import json
yield f"data: {json.dumps(chunk_dict)}\n\n".encode('utf-8')
except Exception as chunk_error:
# Handle errors during chunk serialization (e.g., tool calls without tool_choice)
logger.warning(f"Error serializing chunk: {str(chunk_error)}")
# Skip this chunk and continue with the next one
continue
except Exception as e: except Exception as e:
logger.error(f"Error in streaming response: {str(e)}") logger.error(f"Error in streaming response: {str(e)}")
import json import json
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment