Skip to content

Python: support typing.NewType in schema conversion - #458

Open
David Billiot (WeAreResilience) wants to merge 7 commits into
microsoft:mainfrom
WeAreResilience:python-newtype-support
Open

David Billiot (WeAreResilience) wants to merge 7 commits into
microsoft:mainfrom
WeAreResilience:python-newtype-support

Conversation

@WeAreResilience

@WeAreResilience David Billiot (WeAreResilience) commented Oct 13, 2026 •

Copy link
Copy Markdown

Fixes #455

Problem: python_type_to_typescript_schema rejects typing.NewType annotations ('...UserId' cannot be used as a type annotation.), so a translator can't be built for an otherwise valid JSON-shaped type, even though Pydantic validates it.

Fix: In convert_to_type_node, a NewType (typing or typing_extensions) is converted via its __supertype__. This also handles a NewType built on another NewType, and keeps the underlying type's constraint instead of falling back to any. A NewType over an unsupported type still reports a conversion error. Runtime validation is unchanged.

Tests: Added tests/test_newtype_1.py with a schema snapshot covering str/int NewTypes, a NewType built on another, list, dict, | None and NotRequired fields; validator tests (valid values accepted, wrong types rejected); and an unsupported-underlying-type error test. Added test_translator_with_newtype (FixedModel offline translation) so the prompt schema shows NewType fields as their underlying TypeScript types (user_id: string). Documented NewType support in site/src/docs/python/basic-usage.md. pytest passes on 3.11–3.14; pyright reports no new errors.

@WeAreResilience

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Resilience Agentic Solutions LLC"

@robgruen robgruen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, thanks for the change.

Can we please add an offline translation test to verify and update the documentation indicating we now support NewType and how it's expected to work.

Add a FixedModel translator snapshot so NewType fields show as their
underlying TypeScript types in the prompt schema, and document NewType
support in the Python basic-usage guide.
@WeAreResilience

Copy link
Copy Markdown
Author

Added in 996e22e: test_translator_with_newtype (FixedModel offline snapshot; NewType fields emit as underlying TS types in the prompt schema) and a short NewType note in site/src/docs/python/basic-usage.md. Full pytest suite is green.

@robgruen

Copy link
Copy Markdown
Contributor

There was a chicken and egg problem with getting your PR to build so I created a def for NewIsh here (I didn't commit to your fork). Can you please resolve the build error and consolidate your test into test_newtype.py. Sorry for making extra work.

@WeAreResilience

Copy link
Copy Markdown
Author

Thanks for merging main and adding NewTypeish. I removed our duplicate NewTypeish so the one from main is used (the NewType branch now goes through is_new_type), and consolidated the NewType tests into python/tests/test_newtype.py. Pyright 1.1.358 passes on 3.11-3.14 and the full test suite passes locally.

@WeAreResilience

Copy link
Copy Markdown
Author

Thanks for merging main and adding NewTypeish. The offline translation test is in test_translator_with_newtype (FixedModel snapshot; NewType fields show as their underlying TypeScript types in the prompt schema), NewType support is documented in site/src/docs/python/basic-usage.md, and the schema/validator coverage is consolidated in python/tests/test_newtype.py using NewTypeish from main. Pyright 1.1.358 and the full pytest suite pass locally. Happy to adjust further if anything else is needed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support JSON-compatible NewType annotations in Python prompt schemas

2 participants