Skip to content

fix(py): statically narrow response.output as OutputT - #7138

Closed
huangjeff5 wants to merge 1 commit into
fix/py-1.0-pr2-output-hydrationfrom
fix/py-1.0-pr2-output-narrowing
Closed

huangjeff5 wants to merge 1 commit into
fix/py-1.0-pr2-output-hydrationfrom
fix/py-1.0-pr2-output-narrowing

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

Statically narrows response.output to OutputT when output_schema is supplied, stacked on output hydration.

Main Changes

  • Statically narrowed output property: res.output is typed as OutputT rather than Any | None when output_schema is a type.
  • Chunk output narrowing: chunk.output mirrors the narrowed type during streaming.

With output_schema=T, ModelResponse[T].output was typed T | None, so every
caller needed a None guard or cast() before reading a field. It now types
as T for generate(), generate_stream()'s final response, and prompts.
With no schema or a JSON-schema dict it types as Any (was Any | None,
which made pyright, mypy, pyrefly and ty all reject res.output['key']).
Bare ModelResponse keeps OutputT's default, object.

ModelResponseChunk.output stays OutputT | None: early chunks routinely
have nothing to parse. It moves from a cached_property to a property over
a cached helper because ty drops the class type parameter from a
cached_property's return type and inferred chunk.output as None.

Runtime is unchanged. output is still None when the reply carries no
output (interrupted / blocked / other finish, a stream stopped early, or
a boxed failure); the docstring lists those. Pairs with raise_on_error
(PR0), which makes failed turns and INVALID_OUTPUT raise instead.

Typing contract: tests/typecheck/output_narrowing_contract_test.py
(pyright, pyrefly, ty, and pytest).

@gemini-code-assist gemini-code-assist Bot 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 Review

This pull request updates the typing of ModelResponse.output so that when output_schema=T is provided, the output is typed directly as T instead of T | None, removing the need for unnecessary guards or casts. Stream chunks (ModelResponseChunk.output) remain typed as OutputT | None. The changes include updated documentation, refactored property getters, updated tests, and a new type-checking contract test suite. The review feedback suggests a minor grammatical improvement in the documentation to use "is typed as" instead of "types as".


## Typed `output`

With `output_schema=T`, `response.output` types as `T` instead of `T | None`, for `generate`, `generate_stream`'s final response, and prompts. Read it without a guard or `cast()`. With no schema, or a JSON-schema dict, it types as `Any`. `chunk.output` stays `T | None`: early chunks have nothing to parse yet.

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.

medium

For better grammatical correctness and professional tone in the documentation, consider using "is typed as" instead of "types as".

Suggested change
With `output_schema=T`, `response.output` types as `T` instead of `T | None`, for `generate`, `generate_stream`'s final response, and prompts. Read it without a guard or `cast()`. With no schema, or a JSON-schema dict, it types as `Any`. `chunk.output` stays `T | None`: early chunks have nothing to parse yet.
With `output_schema=T`, `response.output` is typed as `T` instead of `T | None`, for `generate`, `generate_stream`'s final response, and prompts. Read it without a guard or `cast()`. With no schema, or a JSON-schema dict, it is typed as `Any`. `chunk.output` stays `T | None`: early chunks have nothing to parse yet.

@huangjeff5

Copy link
Copy Markdown
Contributor Author

Closing: migrated to standalone repository genkit-ai/genkit-python (PR #56): genkit-ai/genkit-python#56

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.

1 participant