fix(llmobs): guard against None candidates/usage in google_genai extractor#18446
Open
nileshpatil6 wants to merge 1 commit into
Open
fix(llmobs): guard against None candidates/usage in google_genai extractor#18446nileshpatil6 wants to merge 1 commit into
nileshpatil6 wants to merge 1 commit into
Conversation
…_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>
emmettbutler
requested changes
Jun 3, 2026
Collaborator
emmettbutler
left a comment
There was a problem hiding this comment.
Seems like this could use a regression test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #18377
The google_genai LLMObs extractor crashes with
Error extracting LLMObs fieldswhen Google GenAI returns partial response objects. This happens in two places:extract_generation_metrics_google_genaiingoogle_utils.py: the fallback computationinput_tokens + output_tokenson line 143 throwsTypeErrorwhen either value isNone. This occurs on streaming chunks where usage metadata fields may be absent._extract_output_messagesingoogle_genai.py:_get_attr(response, "candidates", [])returnsNone(not[]) when the attribute exists but holdsNone, causingTypeErroron iteration. This occurs on safety-filtered responses.Changes:
extract_generation_metrics_google_genai: replaced the one-lineror input_tokens + output_tokensfallback with an explicit None guard sototal_tokensis only computed when both operands are available._extract_output_messages: changed_get_attr(response, "candidates", [])to_get_attr(response, "candidates", None) or []so aNoneattribute value is normalised to an empty list before iteration.Testing
Manual logic test against both None scenarios passes. The existing test suite for
google_genaiLLMObs 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.