Fix: Extract text from nested Google GenAI response structure

- Fixed GoogleProviderHandler to properly extract text from response.candidates[0].content.parts[0].text
- Added error handling for text extraction
- Resolves client error 'Cannot read properties of undefined (reading '0')'
- The Google GenAI SDK returns nested response structure, not direct .text property
parent 871fcdaf
...@@ -152,6 +152,19 @@ class GoogleProviderHandler(BaseProviderHandler): ...@@ -152,6 +152,19 @@ class GoogleProviderHandler(BaseProviderHandler):
logging.info(f"GoogleProviderHandler: Response received: {response}") logging.info(f"GoogleProviderHandler: Response received: {response}")
self.record_success() self.record_success()
# Extract text from the nested response structure
# The response has candidates[0].content.parts[0].text
response_text = ""
try:
if hasattr(response, 'candidates') and response.candidates:
candidate = response.candidates[0]
if hasattr(candidate, 'content') and candidate.content:
if hasattr(candidate.content, 'parts') and candidate.content.parts:
response_text = candidate.content.parts[0].text
except Exception as e:
logging.warning(f"GoogleProviderHandler: Could not extract text from response: {e}")
response_text = ""
# Return the response in OpenAI-style format # Return the response in OpenAI-style format
return { return {
"id": f"google-{model}-{int(time.time())}", "id": f"google-{model}-{int(time.time())}",
...@@ -162,7 +175,7 @@ class GoogleProviderHandler(BaseProviderHandler): ...@@ -162,7 +175,7 @@ class GoogleProviderHandler(BaseProviderHandler):
"index": 0, "index": 0,
"message": { "message": {
"role": "assistant", "role": "assistant",
"content": response.text "content": response_text
}, },
"finish_reason": "stop" "finish_reason": "stop"
}], }],
......
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