Skip to content

fix(types): prevent Unset serializer from matching non-Unset values in unions - #1432

Open
alexb-openrouter wants to merge 1 commit into
mainfrom
fix/unset-union-serialization
Open

alexb-openrouter wants to merge 1 commit into
mainfrom
fix/unset-union-serialization

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix a bug where OptionalNullable[T] fields containing a model with __pydantic_extra__ (such as GuardrailModerationPolicyConfig.armor when Armor.classifier_context is populated) were silently dropped during serialization, causing create_moderation_policy and update_moderation_policy to omit armor.

Root cause

Unset.serialize_model uses @model_serializer(mode="plain") and unconditionally returned UNSET_SENTINEL without checking isinstance(self, Unset). During Pydantic v2 union serialization for OptionalNullable[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 the Unset branch, which accepted the non-Unset model and serialized it as UNSET_SENTINEL (which serialize_model then stripped).

Changes

  • src/openrouter/types/basemodel.py (and .speakeasy/patches/src/openrouter/types/basemodel.py.patch to preserve across SDK regeneration): raise PydanticSerializationUnexpectedValue in Unset.serialize_model when self is not an Unset instance.
  • tests/test_moderation_policy_armor.py: add unit tests covering GuardrailModerationPolicyConfig round-tripping (armor with mode, on_error, classifier_context.auto_mode.hard_deny, extra properties, None, and UNSET) and serialization through both create_moderation_policy and update_moderation_policy.

Verification

  • uv run --group dev pytest (28 passed)
  • uv run --group dev mypy src/openrouter/types/basemodel.py
  • uv run --group dev pyright src/openrouter/types/basemodel.py
  • uv run --group dev pylint src/openrouter/types/basemodel.py --rcfile pylintrc
  • uv build
@devin-ai-integration

devin-ai-integration Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant