Repository navigation
fix(py): statically narrow response.output as OutputT - #7138
huangjeff5 wants to merge 1 commit into
Conversation
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).
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
For better grammatical correctness and professional tone in the documentation, consider using "is typed as" instead of "types as".
| 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. |
|
Closing: migrated to standalone repository genkit-ai/genkit-python (PR #56): genkit-ai/genkit-python#56 |
Statically narrows
response.outputtoOutputTwhenoutput_schemais supplied, stacked on output hydration.Main Changes
outputproperty:res.outputis typed asOutputTrather thanAny | Nonewhenoutput_schemais a type.chunk.outputmirrors the narrowed type during streaming.