Skip to content

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

@yoavf

Description

@yoavf

Tracer Version(s)

4.10 (confirmed on 4.8.7 as well)

Python Version(s)

3.11

Pip Version(s)

pip 26.1

Bug Report

ddtrace==4.10 logs Error extracting LLMObs fields for google_genai.request spans when Google GenAI returns partial response objects where response.candidates or token usage fields are None.

There are two paths where this can manifest:

https://github.com/DataDog/dd-trace-py/blob/v4.10.0/ddtrace/llmobs/_integrations/google_utils.py#L133 reads input_tokens as nullable, and then if total_token_count is falsy, it is summed with output_tokens without checking if it is null first (L144) . This will trigger: TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

https://github.com/DataDog/dd-trace-py/blob/v4.10.0/ddtrace/llmobs/_integrations/google_genai.py#L139 gets response.candidates with default [], but does not normalize the actual value when it is explicitly None, so for candidate in candidates (L144) crashes with TypeError: 'NoneType' object is not iterable

In google-genai==2.6.0, these fields are optional/default None:

from google.genai import types

types.GenerateContentResponse.model_fields["candidates"].annotation
# Optional[list[Candidate]]

types.GenerateContentResponseUsageMetadata.model_fields["prompt_token_count"].annotation
# Optional[int]

types.GenerateContentResponseUsageMetadata.model_fields["candidates_token_count"].annotation
# Optional[int]

types.GenerateContentResponseUsageMetadata.model_fields["total_token_count"].annotation
# Optional[int]

Reproduction Code

from types import SimpleNamespace

from ddtrace.llmobs._integrations.google_utils import (
    extract_generation_metrics_google_genai,
)
from ddtrace.llmobs._integrations.google_genai import GoogleGenAIIntegration

extract_generation_metrics_google_genai(
    SimpleNamespace(
        usage_metadata=SimpleNamespace(
            prompt_token_count=None,
            candidates_token_count=None,
            thoughts_token_count=None,
            total_token_count=None,
        )
    )
)
# TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

integration = object.__new__(GoogleGenAIIntegration)
integration._extract_output_messages(SimpleNamespace(candidates=None))
# TypeError: 'NoneType' object is not iterable

Error Logs

No response

Libraries in Use

google-genai==2.6.0

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions