Skip to content

fix(llmobs): guard against None candidates/usage in google_genai extractor#18446

Open
nileshpatil6 wants to merge 1 commit into
DataDog:mainfrom
nileshpatil6:fix/llmobs-google-genai-none-candidates
Open

fix(llmobs): guard against None candidates/usage in google_genai extractor#18446
nileshpatil6 wants to merge 1 commit into
DataDog:mainfrom
nileshpatil6:fix/llmobs-google-genai-none-candidates

Conversation

@nileshpatil6
Copy link
Copy Markdown

Description

Fixes #18377

The google_genai LLMObs extractor crashes with Error extracting LLMObs fields when Google GenAI returns partial response objects. This happens in two places:

  1. extract_generation_metrics_google_genai in google_utils.py: the fallback computation input_tokens + output_tokens on line 143 throws TypeError when either value is None. This occurs on streaming chunks where usage metadata fields may be absent.

  2. _extract_output_messages in google_genai.py: _get_attr(response, "candidates", []) returns None (not []) when the attribute exists but holds None, causing TypeError on iteration. This occurs on safety-filtered responses.

Changes:

  • In extract_generation_metrics_google_genai: replaced the one-liner or input_tokens + output_tokens fallback with an explicit None guard so total_tokens is only computed when both operands are available.
  • In _extract_output_messages: changed _get_attr(response, "candidates", []) to _get_attr(response, "candidates", None) or [] so a None attribute value is normalised to an empty list before iteration.

Testing

Manual logic test against both None scenarios passes. The existing test suite for google_genai LLMObs integration continues to cover the normal (non-None) path.

Risks

Low. Both changes only affect the None/missing-field code path that was previously crashing. Normal responses with populated fields follow the same logic as before.

Additional Notes

No behaviour change for responses that have valid candidates and token counts.

…_genai extractor

Google GenAI can return partial response objects where candidates or
usage_metadata fields are None (e.g. streaming chunks, safety-filtered
responses). The LLMObs extractor accessed these fields without None
guards, causing a TypeError crash logged as extraction error on every
such response.

- In extract_generation_metrics_google_genai: compute total_tokens
  only when both input_tokens and output_tokens are available, rather
  than unconditionally doing input_tokens + output_tokens which crashes
  when either is None.
- In _extract_output_messages: normalize candidates to [] when the
  attribute exists but is None, preventing TypeError on iteration.

Fixes DataDog#18377

Signed-off-by: nileshpatil6 <technil6436@gmail.com>
@nileshpatil6 nileshpatil6 requested review from a team as code owners June 3, 2026 17:13
Copy link
Copy Markdown
Collaborator

@emmettbutler emmettbutler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could use a regression test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: LLMObs google_genai extractor crashes on optional candidates / usage token fields

2 participants