You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Compaction follow-up: JSON-serialise shrunk tool returns, strengthen guard test, DRY threshold (#1824)
Code-review follow-up for the in-run history compaction processor (#1817).
- Serialise non-string ToolReturnPart content with json.dumps (not str())
when shrinking, via a shared _stringify_tool_content helper that is reused
by the token estimator so the pre-shrink estimate and post-shrink payload
are serialised identically. Matches the existing
json.dumps(..., default=str) convention used for tool-result content
elsewhere in pydantic_ai_agents.py. (finding #2)
- Strengthen test_thinking_only_modelresponse_is_not_emptied: place a large
ToolReturnPart in the older prefix so the empty-parts guard -- not the
no-op early return -- is the verified reason the thinking-only ModelResponse
survives. Add test_non_string_tool_return_serialized_as_json to pin the
JSON-serialisation fix. (finding #1)
- DRY: new context_window_and_threshold() is the single definition of the
compaction kick-in point, shared verbatim by should_compact,
compact_message_history, and the in-run shrink processor (replaces three
copies of int(get_context_window_for_model(...) * ratio)).
- Document that IN_RUN_TOOL_RETURN_TARGET_CHARS / _shrink_tool_return_part
bound the preserved prefix, not the final string (the trim notice adds a
small fixed overhead) (finding #3), and add a comment confirming the
_on_in_run_shrink closure's captured message IDs are stable for the turn
(finding #5).
Findings #4 (test reaching into a pydantic-ai internal) intentionally left as
documented in the issue -- no change needed.
* Address review: guard RecursionError in tool-content stringify, drop stale line refs
- _stringify_tool_content now also catches RecursionError so a (pathological)
self-referential tool return falls back to str() instead of crashing the
shrink/estimate — the stated 'never crash on exotic content' contract.
- Drop the hardcoded 'lines ~866-889' from the closure-stability comment in
pydantic_ai_agents.py (would drift); say 'earlier in this function'.
- Add the symmetric positive assertion (json null present) to
test_non_string_tool_return_serialized_as_json.
* Address PR #1853 review: clarify _stringify_tool_content except + 'None' assertion comments
- Keep the broad except in _stringify_tool_content but correct the
misleading comment: TypeError/ValueError/RecursionError are all genuinely
reachable (default=str does not rescue non-string dict keys, NaN/Inf with a
strict encoder, or circular refs), so narrowing to RecursionError would
turn a graceful fallback into a crash (review nit #1).
- Document why 'None' not in part.content is safe: the only non-JSON text
spliced in is the fixed trim notice, which contains no literal 'None'
(review nit #2).
---------
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments