Skip to content

TypeError: BaseModel.model_dump() missing 'self' when using output_schema with OpenTelemetry enabled #3884

@soundTricker

Description

@soundTricker

Describe the bug
When running an LlmAgent with a Pydantic-based output_schema and OpenTelemetry enabled (--otel_to_cloud), the application crashes with a TypeError: BaseModel.model_dump() missing 1 required positional argument: 'self'.

The error originates from opentelemetry-instrumentation-google-genai, specifically within dict_util.py.

To Reproduce
Steps to reproduce the behavior:

  1. Create a minimal agent code with output_schema (time_agent/agent.py):
from google.adk.agents.llm_agent import Agent
from pydantic import BaseModel
from datetime import datetime

class TimeRes(BaseModel):
    time: str

def now_tool():
    """
    Get the current time in the format of %Y-%m-%d %H:%M:%S
    """
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

root_agent = Agent(
    model='gemini-2.5-flash',
    name='TimeAgent',
    description='An agent that returns the current time.',
    instruction='Return the current time in the format of %Y-%m-%d %H:%M:%S by using `now_tool`',
    output_schema=TimeRes,
    tools=[now_tool]
)
  1. Run adk web with OpenTelemetry enabled:
adk web . --otel_to_cloud
  1. Execute a query to the agent via the web UI or API.

  2. The following error stacktrace is produced:

2025-12-10 16:30:30,405 - ERROR - adk_web_server.py:1511 - Error in event_generator: BaseModel.model_dump() missing 1 required positional argument: 'self'
Traceback (most recent call last):
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/google/adk/cli/adk_web_server.py", line 1501, in event_generator
    async for event in agen:
  ...
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/google/adk/models/google_llm.py", line 211, in generate_content_async
    response = await self.api_client.aio.models.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 885, in instrumented_generate_content
    helper.add_request_options_to_span(config)
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 379, in add_request_options_to_span
    _add_request_options_to_span(
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 187, in _add_request_options_to_span
    attributes = flatten_dict(
                 ^^^^^^^^^^^^^
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/dict_util.py", line 295, in flatten_dict
    return _flatten_dict(
           ^^^^^^^^^^^^^^
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/dict_util.py", line 251, in _flatten_dict
    flattened = _flatten_value(
                ^^^^^^^^^^^^^^^
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/dict_util.py", line 228, in _flatten_value
    return _flatten_compound_value(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/git/work/adk-otel-issue/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/dict_util.py", line 193, in _flatten_compound_value
    value.model_dump(),
    ^^^^^^^^^^^^^^^^^^
TypeError: BaseModel.model_dump() missing 1 required positional argument: 'self'

Expected behavior
The agent should execute successfully, return the time, and send traces to Cloud Trace without crashing.

Screenshots
N/A

Desktop:

  • OS: macOS / Cloud Run
  • Python version: 3.12.0
  • ADK version: 1.20.0 (install google-adk[otel-gcp] or opentelemetry-instrumentation-google-genai=0.4b0

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: gemini-2.5-flash

Additional context

Workaround:
Removing opentelemetry-instrumentation-google-genai from the dependencies prevents this error.
If you use uv, add below to pyproject.toml.

[tool.uv]
override-dependencies = [
"opentelemetry-instrumentation-google-genai ; sys_platform == 'never'"
]

Suspected Root Cause:
In llm_request.py, the response_schema property of config (types.GenerateContentConfig) is directly assigned the Pydantic model class (type) derived from output_schema.

https://github.com/google/adk-python/blob/main/src/google/adk/models/llm_request.py#L275C17-L275C32

When opentelemetry-instrumentation-google-genai attempts to dump types.GenerateContentConfig to send to OTel, it seems to encounter this response_schema. Since it is a type[BaseModel] (a class) and not an instance, calling model_dump() (which requires an instance in Pydantic V2) causes the TypeError.

Note: While the exception occurs in the instrumentation library, I am reporting it here because ADK is passing the class type in the config, which triggers the issue. Debugging this took significant time, so I wanted to share these findings.

Metadata

Metadata

Labels

tracing[Component] This issue is related to OpenTelemetry tracing

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions