Commit f665331
fix(utils): Avoid double serialization of strings in safe_serialize (#5587)
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
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 5db5874 commit f665331
File tree
4 files changed
+46
-13
lines changed- sentry_sdk
- tests
- integrations
- google_genai
- mcp
4 files changed
+46
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2005 | 2005 | | |
2006 | 2006 | | |
2007 | 2007 | | |
2008 | | - | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
2009 | 2013 | | |
2010 | 2014 | | |
2011 | 2015 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1625 | 1625 | | |
1626 | 1626 | | |
1627 | 1627 | | |
1628 | | - | |
| 1628 | + | |
1629 | 1629 | | |
1630 | 1630 | | |
1631 | 1631 | | |
| |||
1891 | 1891 | | |
1892 | 1892 | | |
1893 | 1893 | | |
1894 | | - | |
| 1894 | + | |
1895 | 1895 | | |
1896 | 1896 | | |
1897 | 1897 | | |
| |||
1908 | 1908 | | |
1909 | 1909 | | |
1910 | 1910 | | |
1911 | | - | |
| 1911 | + | |
1912 | 1912 | | |
1913 | 1913 | | |
1914 | 1914 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
244 | | - | |
| 244 | + | |
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
248 | | - | |
249 | 248 | | |
250 | | - | |
251 | | - | |
252 | | - | |
| 249 | + | |
253 | 250 | | |
254 | 251 | | |
255 | 252 | | |
| |||
366 | 363 | | |
367 | 364 | | |
368 | 365 | | |
369 | | - | |
370 | | - | |
| 366 | + | |
| 367 | + | |
371 | 368 | | |
372 | 369 | | |
373 | 370 | | |
| |||
752 | 749 | | |
753 | 750 | | |
754 | 751 | | |
755 | | - | |
| 752 | + | |
756 | 753 | | |
757 | 754 | | |
758 | 755 | | |
| |||
959 | 956 | | |
960 | 957 | | |
961 | 958 | | |
962 | | - | |
| 959 | + | |
963 | 960 | | |
964 | 961 | | |
965 | 962 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
1062 | 1063 | | |
1063 | 1064 | | |
1064 | 1065 | | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
1065 | 1097 | | |
1066 | 1098 | | |
0 commit comments