Skip to content

fix(utils): Avoid double serialization of strings in safe_serialize#5587

Open
ericapisani wants to merge 3 commits intomasterfrom
ep/fix-double-serialization-in-pydantic-ai-tools-62h
Open

fix(utils): Avoid double serialization of strings in safe_serialize#5587
ericapisani wants to merge 3 commits intomasterfrom
ep/fix-double-serialization-in-pydantic-ai-tools-62h

Conversation

@ericapisani
Copy link
Member

@ericapisani ericapisani commented Mar 4, 2026

When serialize_item() inside safe_serialize() already returns a string
(for plain strings, callables, or objects with __dict__), the subsequent
json.dumps() call wraps it in extra quotes with escaped characters. For
example, a JSON string '{"param": "value"}' becomes
'"{\\"param\\": \\"value\\"}"'.

This was causing double-serialized tool arguments in pydantic-ai
integrations.

The fix skips json.dumps when serialize_item has already produced a
string, and adds tests covering plain strings, JSON strings, dicts,
callables, and objects.

Part of PY-2114

When serialize_item() already returns a string (for plain strings,
callables, or objects with __dict__), skip json.dumps to prevent
wrapping the value in extra quotes with escaped characters.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • (celery) Propagate user-set headers by sentrivana in #5581
  • (utils) Avoid double serialization of strings in safe_serialize by ericapisani in #5587

Documentation 📚

  • Add AGENTS.md by sentrivana in #5579
  • Add set_attribute example to changelog by sentrivana in #5578

🤖 This preview updates automatically when you update the PR.

@linear
Copy link

linear bot commented Mar 4, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 12.55s

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 13794 uncovered lines.

Files with missing lines (1)
File Patch % Lines
utils.py 52.63% ⚠️ 432 Missing and 79 partials

Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Codecov Results 📊

4 passed | Total: 4 | Pass Rate: 100% | Execution Time: 624ms

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -3
Passed Tests 📉 -3
Failed Tests
Skipped Tests

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 15445 uncovered lines.
❌ Project coverage is 21.9%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
utils.py 35.57% ⚠️ 587 Missing and 54 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    22.41%    21.90%    -0.51%
==========================================
  Files          189       189         —
  Lines        19902     19777      -125
  Branches      6464      6456        -8
==========================================
+ Hits          4461      4332      -129
- Misses       15441     15445        +4
- Partials       374       374         —

Generated by Codecov Action

ericapisani and others added 2 commits March 4, 2026 14:53
Remove wrapping double quotes from expected tool output strings in
google_genai tests. The safe_serialize fix (5937e9c) now correctly
returns plain strings without passing them through json.dumps.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove extra quoting from string argument assertions and fix
double-serialized tool result expectations. The safe_serialize fix in
5937e9c eliminated double serialization of strings, so test
expectations need to match the new single-serialized output.

Co-Authored-By: Claude <noreply@anthropic.com>
@ericapisani ericapisani marked this pull request as ready for review March 4, 2026 20:11
@ericapisani ericapisani requested a review from a team as a code owner March 4, 2026 20:11
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.

1 participant