OTEL span attributes fail with dict type for llm_request/llm_response
Description
When running agents on Vertex AI Agent Engine (Reasoning Engine), OpenTelemetry tracing emits warnings about invalid attribute types for LLM request/response data.
Warning Messages
WARNING: Invalid type dict for attribute 'gcp.vertex.agent.llm_request' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
WARNING: Invalid type dict for attribute 'gcp.vertex.agent.llm_response' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Environment
- ADK Version: google-adk 1.21.0
- Platform: Vertex AI Agent Engine (Reasoning Engine)
- Python: 3.12
- Region: us-central1
Root Cause
OpenTelemetry span attributes only support primitive types (bool, str, bytes, int, float) or sequences of those types. The ADK tracing code is attempting to set gcp.vertex.agent.llm_request and gcp.vertex.agent.llm_response as dict objects, which OTEL rejects.
Expected Behavior
LLM request/response data should either be:
- Serialized to JSON string before being set as span attributes
- Set as span events instead of attributes (events support structured data)
- Logged separately rather than attached to spans
Impact
- Warnings clutter logs on every LLM call
- Tracing data for LLM requests/responses may be incomplete or missing in Cloud Trace
- Not blocking execution but reduces observability quality
Suggested Fix
In the tracing code that sets these attributes, serialize dict values to JSON strings:
import json
# Instead of:
span.set_attribute("gcp.vertex.agent.llm_request", llm_request_dict)
# Use:
span.set_attribute("gcp.vertex.agent.llm_request", json.dumps(llm_request_dict, default=str))
Or use span events for structured data:
span.add_event("llm_request", attributes={"data": json.dumps(llm_request_dict, default=str)})
Reproduction
- Deploy an agent to Vertex AI Agent Engine
- Send a request that triggers an LLM call
- Check Cloud Logging stderr logs for the warning messages
Related Issues
- #4043 - Similar serialization issue with
bytestype forthought_signature
Both issues stem from ADK's telemetry not properly handling non-primitive types when setting OTEL span attributes.
@sasha-gitg would this issue be fixed by a similar fix for issue #4043 ?
This seems to be duplicate of https://github.com/google/adk-python/issues/4094 and should be fixed by https://github.com/google/adk-python/commit/5880109ab13456d5ec511e991de9271fbbec41d8.