Repository navigation
fix(types): prevent Unset serializer from matching non-Unset values in unions - #1432
Open
alexb-openrouter wants to merge 1 commit into
Open
alexb-openrouter wants to merge 1 commit into
alexb-openrouter wants to merge 1 commit into
Conversation
Contributor
|
I'll fix CI failures and address comments from users with write access that start with 'Devin'.
|
This branch has not been deployed
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.
Summary
Fix a bug where
OptionalNullable[T]fields containing a model with__pydantic_extra__(such asGuardrailModerationPolicyConfig.armorwhenArmor.classifier_contextis populated) were silently dropped during serialization, causingcreate_moderation_policyandupdate_moderation_policyto omitarmor.Root cause
Unset.serialize_modeluses@model_serializer(mode="plain")and unconditionally returnedUNSET_SENTINELwithout checkingisinstance(self, Unset). During Pydantic v2 union serialization forOptionalNullable[T](Union[Optional[Nullable[T]], Unset]), models with__pydantic_extra__: Dict[str, Any](extra="allow") fail Pydantic's strict first-pass union check, so Pydantic falls through to theUnsetbranch, which accepted the non-Unsetmodel and serialized it asUNSET_SENTINEL(whichserialize_modelthen stripped).Changes
src/openrouter/types/basemodel.py(and.speakeasy/patches/src/openrouter/types/basemodel.py.patchto preserve across SDK regeneration): raisePydanticSerializationUnexpectedValueinUnset.serialize_modelwhenselfis not anUnsetinstance.tests/test_moderation_policy_armor.py: add unit tests coveringGuardrailModerationPolicyConfiground-tripping (armorwithmode,on_error,classifier_context.auto_mode.hard_deny, extra properties,None, andUNSET) and serialization through bothcreate_moderation_policyandupdate_moderation_policy.Verification
uv run --group dev pytest(28 passed)uv run --group dev mypy src/openrouter/types/basemodel.pyuv run --group dev pyright src/openrouter/types/basemodel.pyuv run --group dev pylint src/openrouter/types/basemodel.py --rcfile pylintrcuv build