Skip to content

GoogleGenAiSdkInstrumentor: JSON Serialization Error with Part Objects in system_instruction #3979

@alexmojaki

Description

@alexmojaki

Originally reported in pydantic/logfire#1547

import os

from google import genai
from google.genai import types
from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor

os.environ['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT'] = 'true'
GoogleGenAiSdkInstrumentor().instrument()

client = genai.Client()
config = types.GenerateContentConfig(system_instruction=[types.Part.from_text(text='help')])
response = client.models.generate_content(model='gemini-2.0-flash-exp', contents=['hi'], config=config)
Traceback (most recent call last):
  File "/Users/alex/Library/Application Support/JetBrains/PyCharm2025.2/scratches/scratch_2463.py", line 12, in <module>
    response = client.models.generate_content(model='gemini-2.0-flash-exp', contents=['hi'], config=config)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 763, in instrumented_generate_content
    helper.process_request(contents, config)
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 388, in process_request
    self._maybe_log_system_instruction(config=config)
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 567, in _maybe_log_system_instruction
    body["content"] = _to_dict(system_instruction)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/google_genai/generate_content.py", line 166, in _to_dict
    return json.loads(json.dumps(value))
                      ^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Part is not JSON serializable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions