Add debug logging for streaming chunk serialization errors

- Log chunk type and content before serialization attempt
- Log chunk type and content when serialization fails
- Helps diagnose 'Tool choice is none, but model called a tool' errors
- Apply debug logging to both RequestHandler and AutoselectHandler streaming methods
parent fccf6bca
...@@ -109,6 +109,8 @@ class RequestHandler: ...@@ -109,6 +109,8 @@ class RequestHandler:
raise HTTPException(status_code=503, detail="Provider temporarily unavailable") raise HTTPException(status_code=503, detail="Provider temporarily unavailable")
async def stream_generator(): async def stream_generator():
import logging
logger = logging.getLogger(__name__)
try: try:
# Apply rate limiting # Apply rate limiting
await handler.apply_rate_limit() await handler.apply_rate_limit()
...@@ -122,14 +124,19 @@ class RequestHandler: ...@@ -122,14 +124,19 @@ class RequestHandler:
) )
for chunk in response: for chunk in response:
try: try:
# Debug: Log chunk type and content before serialization
logger.debug(f"Chunk type: {type(chunk)}")
logger.debug(f"Chunk: {chunk}")
# Convert chunk to dict and serialize as JSON # Convert chunk to dict and serialize as JSON
chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk
import json import json
yield f"data: {json.dumps(chunk_dict)}\n\n".encode('utf-8') yield f"data: {json.dumps(chunk_dict)}\n\n".encode('utf-8')
except Exception as chunk_error: except Exception as chunk_error:
# Handle errors during chunk serialization (e.g., tool calls without tool_choice) # Handle errors during chunk serialization (e.g., tool calls without tool_choice)
import logging logger.warning(f"Error serializing chunk: {str(chunk_error)}")
logging.warning(f"Error serializing chunk: {str(chunk_error)}") logger.warning(f"Chunk type: {type(chunk)}")
logger.warning(f"Chunk content: {chunk}")
# Skip this chunk and continue with the next one # Skip this chunk and continue with the next one
continue continue
handler.record_success() handler.record_success()
...@@ -658,6 +665,10 @@ class AutoselectHandler: ...@@ -658,6 +665,10 @@ class AutoselectHandler:
) )
for chunk in response: for chunk in response:
try: try:
# Debug: Log chunk type and content before serialization
logger.debug(f"Chunk type: {type(chunk)}")
logger.debug(f"Chunk: {chunk}")
# Convert chunk to dict and serialize as JSON # Convert chunk to dict and serialize as JSON
chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk chunk_dict = chunk.model_dump() if hasattr(chunk, 'model_dump') else chunk
import json import json
...@@ -665,6 +676,8 @@ class AutoselectHandler: ...@@ -665,6 +676,8 @@ class AutoselectHandler:
except Exception as chunk_error: except Exception as chunk_error:
# Handle errors during chunk serialization (e.g., tool calls without tool_choice) # Handle errors during chunk serialization (e.g., tool calls without tool_choice)
logger.warning(f"Error serializing chunk: {str(chunk_error)}") logger.warning(f"Error serializing chunk: {str(chunk_error)}")
logger.warning(f"Chunk type: {type(chunk)}")
logger.warning(f"Chunk content: {chunk}")
# Skip this chunk and continue with the next one # Skip this chunk and continue with the next one
continue continue
except Exception as e: except Exception as e:
......
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