Repository navigation
fix(py): type prompt on_chunk callbacks as ModelResponseChunk #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1caceff
1363fb4
0df3bdc
18cff5d
43c4f81
0d36862
3bdcf41
047575f
74676ca
fd66387
756770e
2d3806a
c31cc69
848b91d
62ac92f
fbcbdee
d4f9c0c
54115f8
ec7d06d
d784714
2ff7959
a96a08b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # pyright: reportUnnecessaryTypeIgnoreComment=true | ||
|
|
||
| """Type-checker contract for ``Prompt.__call__(on_chunk=...)``. | ||
|
|
||
| The callback receives ``ModelResponseChunk[OutputT]``: ``.text`` reads with no | ||
| cast, a prompt with a typed ``output_schema`` types the chunk's output, a | ||
| callback that takes ``object`` still fits, and one that takes an unrelated | ||
| type is rejected. | ||
| """ | ||
|
|
||
| import pytest | ||
| from pydantic import BaseModel | ||
| from typing_extensions import assert_type | ||
|
|
||
| from genkit import Genkit, Message, ModelResponse, ModelResponseChunk, Part, Role | ||
| from genkit.model import ModelContext, ModelRequest | ||
|
|
||
|
|
||
| class Dish(BaseModel): | ||
| name: str | ||
|
|
||
|
|
||
| def _app() -> Genkit: | ||
| ai = Genkit() | ||
|
|
||
| async def line_cook(req: ModelRequest, ctx: ModelContext) -> ModelResponse: | ||
| del req | ||
| ctx.send_chunk(ModelResponseChunk(content=[Part.from_text('{"name": ')])) | ||
| ctx.send_chunk(ModelResponseChunk(content=[Part.from_text('"tartine"}')])) | ||
| return ModelResponse(message=Message(role=Role.MODEL, content=[Part.from_text('{"name": "tartine"}')])) | ||
|
|
||
| ai.define_model(name='bistro/line-cook', fn=line_cook) | ||
| return ai | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_on_chunk_reads_text_without_a_cast() -> None: | ||
| """``on_chunk=lambda c: ...`` sees a ModelResponseChunk, so ``.text`` type-checks.""" | ||
| ai = _app() | ||
| special = ai.define_prompt(name='special', model='bistro/line-cook', prompt="Today's special?") | ||
| seen: list[str] = [] | ||
|
|
||
| await special(on_chunk=lambda c: seen.append(c.text)) | ||
|
|
||
| assert seen == ['{"name": ', '"tartine"}'] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_on_chunk_is_typed_by_the_output_schema() -> None: | ||
| """A prompt with ``output_schema=Dish`` hands the callback ``ModelResponseChunk[Dish]``.""" | ||
| ai = _app() | ||
| plate = ai.define_prompt(name='plate', model='bistro/line-cook', prompt='Plate it.', output_schema=Dish) | ||
| chunks: list[ModelResponseChunk[Dish]] = [] | ||
|
|
||
| def on_dish(chunk: ModelResponseChunk[Dish]) -> None: | ||
| assert_type(chunk.output, Dish | None) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checks |
||
| chunks.append(chunk) | ||
|
|
||
| await plate(on_chunk=on_dish) | ||
|
|
||
| assert len(chunks) == 2 | ||
| assert all(isinstance(c, ModelResponseChunk) for c in chunks) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_object_callbacks_still_fit_and_unrelated_ones_do_not() -> None: | ||
| """A callback taking ``object`` still fits; one taking an unrelated type doesn't.""" | ||
| ai = _app() | ||
| special = ai.define_prompt(name='special', model='bistro/line-cook', prompt="Today's special?") | ||
| received: list[object] = [] | ||
|
|
||
| # 1. Callable params are contravariant: object accepts every chunk | ||
| def log_anything(chunk: object) -> None: | ||
| received.append(chunk) | ||
|
|
||
| await special(on_chunk=log_anything) | ||
| assert len(received) == 2 | ||
| assert all(isinstance(c, ModelResponseChunk) for c in received) | ||
|
|
||
| # 2. A callback returning a value fits too | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| await special(on_chunk=received.append) | ||
|
|
||
| # 3. A callback for some other type doesn't | ||
| def count_covers(covers: int) -> None: | ||
| del covers | ||
|
|
||
| await special(on_chunk=count_covers) # pyright: ignore[reportArgumentType] # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
ModelResponseChunkis invariant in its output type, a callback typed with bareModelResponseChunk(main's own signature) no longer type-checks on a prompt with anoutput_schema. Forwardingctx.send_chunkfrom aFlowContext[ModelResponseChunk]breaks the same way, in both pyright and ty. So this is breaking for those callers, which the body currently says it isn't.