Skip to content

OTEL span attributes fail with dict type for llm_request/llm_response #4083

@democodex

Description

@democodex

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:

  1. Serialized to JSON string before being set as span attributes
  2. Set as span events instead of attributes (events support structured data)
  3. 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

  1. Deploy an agent to Vertex AI Agent Engine
  2. Send a request that triggers an LLM call
  3. Check Cloud Logging stderr logs for the warning messages

Related Issues

Both issues stem from ADK's telemetry not properly handling non-primitive types when setting OTEL span attributes.

Metadata

Metadata

Labels

planned[Status] This issue is planned to be work on by ADK eng teamtracing[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