fix(serialization): apply exclude recursively for keys nested inside Pydantic models#6595
Open
lntutor wants to merge 1 commit into
Open
fix(serialization): apply exclude recursively for keys nested inside Pydantic models#6595lntutor wants to merge 1 commit into
lntutor wants to merge 1 commit into
Conversation
…Pydantic models to_serializable() forwards exclude recursively for dicts, lists, and dataclasses, but the BaseModel branch only passed exclude to model_dump() -- which drops top-level fields only (a set exclude is not recursive in Pydantic v2) -- and then dropped exclude when recursing into the resulting dict. So an excluded key nested inside a model (e.g. a 'secret' field on a nested Address) leaked into the output, defeating a caller's explicit request to drop a potentially sensitive field. Forward exclude into the model branch's recursive calls so nested exclusion is consistent with every other container type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N6RtoHuxrDqTUo9Mw9h4Cv
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesSerialization exclusions
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's broken
to_serializable(obj, exclude={...})appliesexcluderecursively for dicts (filtered at every level), lists, and dataclasses (both forwardexcludeinto recursive calls). TheBaseModelbranch is the exception: it passesexcludetomodel_dump()— which only drops top-level fields (asetexclude is not recursive in Pydantic v2) — and then recurses into the resulting dict without forwardingexclude:Since
to_serializableserializes agent/task/event state (e.g.to_string), this leaks fields a caller explicitly asked to drop — potentially sensitive data.Fix
Forward
excludeinto theBaseModelbranch's recursive calls (both themodel_dumppath and the__dict__fallback), relying on the existing recursive dict-branch filtering. Top-level exclusion is unchanged.Tests
Extends
lib/crewai/tests/utilities/test_serialization.py: a nested-model exclude case, and a parity check that the model path matches the plain-dict path. Full file passes (27). Distinct from #2479 (which added theexcludeoption).