Skip to content

fix(google-genai): break circular reference in span event body#1883

Open
MukundaKatta wants to merge 1 commit intopydantic:mainfrom
MukundaKatta:fix/google-genai-file-upload-circular
Open

fix(google-genai): break circular reference in span event body#1883
MukundaKatta wants to merge 1 commit intopydantic:mainfrom
MukundaKatta:fix/google-genai-file-upload-circular

Conversation

@MukundaKatta
Copy link
Copy Markdown

Closes #1881.

instrument_google_genai calls json.dumps(body, default=default_json) to attach the user/assistant payload to the span. When upstream _to_dict returns a self-referential dict (for example for an uploaded Gemini File part) Python raises ValueError: Circular reference detected, handle_internal_errors logs it as an internal error, and the span event is dropped silently.

Catch that ValueError and retry on a cycle-stripped copy: a small recursive walker swaps the back-edge with safe_repr so the rest of the body still serializes cleanly. Test in tests/otel_integrations/test_google_genai.py::test_span_event_logger_with_circular_reference.

Upstream `_to_dict` can return self-referential dicts (notably for uploaded
Gemini File parts). `json.dumps` then raises `ValueError: Circular reference
detected`, which `handle_internal_errors` swallows and the span event is
lost. Fall back to a cycle-stripped copy that uses `safe_repr` for the
back-edge so the event still gets emitted.

Closes pydantic#1881.
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

❌ Patch coverage is 68.00000% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
logfire/_internal/integrations/google_genai.py 68.00% 7 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError: Circular reference detected with Gemini + file upload, and logs with large inline data blocking async event loop

1 participant