instructor icon indicating copy to clipboard operation
instructor copied to clipboard

ReadOnly TypedDict response models fail during schema preparation

Open emme1t opened this issue 2 months ago • 0 comments

  • [x] This is actually a bug report.
  • [x] I searched the existing issues and pull requests.

What Model are you using?

Offline schema preparation. The error occurs before a provider request and also affects instructor.from_openai(...).create(response_model=Record, ...).

Describe the bug

A supported TypedDict response model fails to prepare when a field uses the PEP 705 ReadOnly qualifier. _typed_dict_to_model passes this TypedDict-specific qualifier into a Pydantic model field. Pydantic 2.13.5 raises PydanticForbiddenQualifier.

Required[ReadOnly[int]], ReadOnly[Required[int]], and the corresponding NotRequired combinations also fail. The same conversion affects list[Record] response models.

To Reproduce

from typing_extensions import ReadOnly, TypedDict

from instructor.processing.response import handle_response_model


class Record(TypedDict):
    name: ReadOnly[str]


handle_response_model(Record)

Observed exception on the current main revision e12f8b49203b0c1f253d27c1e709d0a09b9fc5a8:

pydantic.errors.PydanticForbiddenQualifier: The annotation 'ReadOnly[str]' contains the 'typing.NotRequired' type qualifier, which is invalid in the context it is defined.

The reference to NotRequired above is Pydantic's actual exception text for this ReadOnly input.

Expected behavior

Prepare a string field while preserving required/optional key semantics and Annotated field constraints. ReadOnly can keep its static typing meaning while the generated model follows its normal Pydantic mutation behavior.

Environment

  • Windows, Python 3.13.13
  • Instructor 1.17.1, installed from the main revision above
  • Pydantic 2.13.5, OpenAI 3.3.0, typing_extensions 4.16.0

The existing optional-key issue #2567 concerns total=False and NotRequired; its merged fix supplies the conversion path used here. Searches for ReadOnly found no matching issue or PR.


This report and reproduction were prepared with Codex as part of an Astra Ultra-based automated PR project that contributes to the open-source community.

emme1t avatar Sep 19 '26 10:09 emme1t