Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions py/packages/genkit/src/genkit/ai/_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async def generate(
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
tool_responses: list[Part] | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
on_chunk: ModelStreamingCallback | None = None,
context: dict[str, object] | None = None,
Expand All @@ -178,7 +178,7 @@ async def generate(
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
tool_responses: list[Part] | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
on_chunk: ModelStreamingCallback | None = None,
context: dict[str, object] | None = None,
Expand All @@ -201,7 +201,7 @@ async def generate(
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
tool_responses: list[Part] | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
on_chunk: ModelStreamingCallback | None = None,
context: dict[str, object] | None = None,
Expand Down Expand Up @@ -358,7 +358,7 @@ def generate_stream(
tools: list[str] | None = None,
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
context: dict[str, object] | None = None,
output_format: str | None = None,
Expand All @@ -385,7 +385,7 @@ def generate_stream(
tools: list[str] | None = None,
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
context: dict[str, object] | None = None,
output_format: str | None = None,
Expand All @@ -410,7 +410,7 @@ def generate_stream(
tools: list[str] | None = None,
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
context: dict[str, object] | None = None,
output_format: str | None = None,
Expand Down Expand Up @@ -1003,7 +1003,7 @@ async def generate_operation(
tools: list[str] | None = None,
return_tool_requests: bool | None = None,
tool_choice: ToolChoice | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
max_turns: int | None = None,
context: dict[str, object] | None = None,
output_format: str | None = None,
Expand Down
10 changes: 5 additions & 5 deletions py/packages/genkit/src/genkit/ai/_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, object] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, object] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1246,7 +1246,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, object] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1276,7 +1276,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, object] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand All @@ -1303,7 +1303,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, object] | None = None,
config: dict[str, object] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, object] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down
18 changes: 9 additions & 9 deletions py/packages/genkit/src/genkit/blocks/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def on_chunk(chunk):
"""

model: str | None
config: GenerationCommonConfig | dict[str, Any] | None
config: dict[str, Any] | GenerationCommonConfig | None
messages: list[Message] | None
docs: list[DocumentData] | None
tools: list[str] | None
Expand Down Expand Up @@ -554,7 +554,7 @@ class PromptConfig(BaseModel):

variant: str | None = None
model: str | None = None
config: GenerationCommonConfig | dict[str, Any] | None = None
config: dict[str, Any] | GenerationCommonConfig | None = None
description: str | None = None
input_schema: type | dict[str, Any] | str | None = None
system: str | Part | list[Part] | Callable[..., Any] | None = None
Expand Down Expand Up @@ -677,7 +677,7 @@ def __init__(
registry: Registry,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, Any] | None = None,
config: dict[str, Any] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, Any] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -731,7 +731,7 @@ def __init__(
self._registry: Registry = registry
self._variant: str | None = variant
self._model: str | None = model
self._config: GenerationCommonConfig | dict[str, Any] | None = config
self._config: dict[str, Any] | GenerationCommonConfig | None = config
self._description: str | None = description
self._input_schema: type | dict[str, Any] | str | None = input_schema
self._system: str | Part | list[Part] | Callable[..., Any] | None = system
Expand Down Expand Up @@ -1185,7 +1185,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, Any] | None = None,
config: dict[str, Any] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, Any] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, Any] | None = None,
config: dict[str, Any] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, Any] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1247,7 +1247,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, Any] | None = None,
config: dict[str, Any] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, Any] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1278,7 +1278,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, Any] | None = None,
config: dict[str, Any] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, Any] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down Expand Up @@ -1307,7 +1307,7 @@ def define_prompt(
name: str | None = None,
variant: str | None = None,
model: str | None = None,
config: GenerationCommonConfig | dict[str, Any] | None = None,
config: dict[str, Any] | GenerationCommonConfig | None = None,
description: str | None = None,
input_schema: type | dict[str, Any] | str | None = None,
system: str | Part | list[Part] | Callable[..., Any] | None = None,
Expand Down
3 changes: 2 additions & 1 deletion py/packages/genkit/src/genkit/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ def dump_json(
if isinstance(obj, BaseModel):
return obj.model_dump_json(by_alias=True, exclude_none=True, indent=indent, fallback=fallback)
else:
return json.dumps(obj, indent=indent, default=fallback or default_serializer)
separators = (',', ':') if indent is None else None
return json.dumps(obj, indent=indent, default=fallback or default_serializer, separators=separators)
4 changes: 2 additions & 2 deletions py/packages/genkit/src/genkit/model_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GenerationCommonConfig(CoreGenerationCommonConfig):
)


def get_request_api_key(config: GenerationCommonConfig | Mapping[str, object] | object | None) -> str | None:
def get_request_api_key(config: Mapping[str, object] | GenerationCommonConfig | object | None) -> str | None:
"""Extract a request-scoped API key from config.

Supports both typed config objects and dict payloads with either snake_case
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_request_api_key(config: GenerationCommonConfig | Mapping[str, object] |


def get_effective_api_key(
config: GenerationCommonConfig | Mapping[str, object] | object | None,
config: Mapping[str, object] | GenerationCommonConfig | object | None,
plugin_api_key: str | None,
) -> str | None:
"""Resolve effective API key using request-over-plugin precedence."""
Expand Down
10 changes: 5 additions & 5 deletions py/packages/genkit/tests/genkit/blocks/prompt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def test_simple_prompt() -> None:
"""Test simple prompt rendering."""
ai, *_ = setup_test()

want_txt = '[ECHO] user: "hi" {"temperature":11.0}'
want_txt = '[ECHO] user: "hi" {"temperature":11}'

my_prompt = ai.define_prompt(prompt='hi', config={'temperature': 11})

Expand All @@ -87,7 +87,7 @@ async def test_simple_prompt_with_override_config() -> None:
ai, *_ = setup_test()

# Config is MERGED: prompt config (banana: true) + opts config (temperature: 12)
want_txt = '[ECHO] user: "hi" {"banana": true, "temperature": 12}'
want_txt = '[ECHO] user: "hi" {"banana":true,"temperature":12}'

my_prompt = ai.define_prompt(prompt='hi', config={'banana': True})

Expand Down Expand Up @@ -236,7 +236,7 @@ async def docs_resolver(input: dict[str, Any], context: object) -> list[Document
GenerationCommonConfig.model_validate({'temperature': 11}),
{},
# Config is MERGED: prompt config (banana: ripe) + opts config (temperature: 11)
"""[ECHO] system: "hello foo (bar)" {"banana": "ripe", "temperature": 11.0}""",
"""[ECHO] system: "hello foo (bar)" {"banana":"ripe","temperature":11.0}""",
),
(
'renders user prompt',
Expand All @@ -256,7 +256,7 @@ async def docs_resolver(input: dict[str, Any], context: object) -> list[Document
GenerationCommonConfig.model_validate({'temperature': 11}),
{},
# Config is MERGED: prompt config (banana: ripe) + opts config (temperature: 11)
"""[ECHO] user: "hello foo (bar_system)" {"banana": "ripe", "temperature": 11.0}""",
"""[ECHO] user: "hello foo (bar_system)" {"banana":"ripe","temperature":11.0}""",
),
(
'renders user prompt with context',
Expand All @@ -276,7 +276,7 @@ async def docs_resolver(input: dict[str, Any], context: object) -> list[Document
GenerationCommonConfig.model_validate({'temperature': 11}),
{'auth': {'email': 'a@b.c'}},
# Config is MERGED: prompt config (banana: ripe) + opts config (temperature: 11)
"""[ECHO] user: "hello foo (bar, a@b.c)" {"banana": "ripe", "temperature": 11.0}""",
"""[ECHO] user: "hello foo (bar, a@b.c)" {"banana":"ripe","temperature":11.0}""",
),
]

Expand Down
6 changes: 3 additions & 3 deletions py/packages/genkit/tests/genkit/codec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
def test_dump_json_basic() -> None:
"""Test basic JSON serialization."""
# Test dictionary
assert dump_json({'a': 1, 'b': 'test'}) == '{"a": 1, "b": "test"}'
assert dump_json({'a': 1, 'b': 'test'}) == '{"a":1,"b":"test"}'

# Test list
assert dump_json([1, 2, 3]) == '[1, 2, 3]'
assert dump_json([1, 2, 3]) == '[1,2,3]'

# Test nested structures
assert dump_json({'a': [1, 2], 'b': {'c': 3}}) == '{"a": [1, 2], "b": {"c": 3}}'
assert dump_json({'a': [1, 2], 'b': {'c': 3}}) == '{"a":[1,2],"b":{"c":3}}'


def test_dump_json_special_types() -> None:
Expand Down
4 changes: 2 additions & 2 deletions py/packages/genkit/tests/genkit/core/action_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ async def baz() -> str:
first = baz_action.arun(context={'foo': 'bar'})
second = baz_action.arun(context={'bar': 'baz'})

assert (await second).response == '{"bar": "baz"}'
assert (await first).response == '{"foo": "bar"}'
assert (await second).response == '{"bar":"baz"}'
assert (await first).response == '{"foo":"bar"}'


@pytest.mark.asyncio
Expand Down
22 changes: 11 additions & 11 deletions py/packages/genkit/tests/genkit/veneer/veneer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def test_generate_uses_default_model(setup_test: SetupFixture) -> None:
"""Test that the generate function uses the default model."""
ai, *_ = setup_test

want_txt = '[ECHO] user: "hi" {"temperature":11.0}'
want_txt = '[ECHO] user: "hi" {"temperature":11}'

response = await ai.generate(prompt='hi', config={'temperature': 11})

Expand Down Expand Up @@ -123,11 +123,11 @@ async def test_generate_with_explicit_model(setup_test: SetupFixture) -> None:

response = await ai.generate(model='echoModel', prompt='hi', config={'temperature': 11})

assert response.text == '[ECHO] user: "hi" {"temperature":11.0}'
assert response.text == '[ECHO] user: "hi" {"temperature":11}'

_, response = ai.generate_stream(model='echoModel', prompt='hi', config={'temperature': 11})

assert (await response).text == '[ECHO] user: "hi" {"temperature":11.0}'
assert (await response).text == '[ECHO] user: "hi" {"temperature":11}'


@pytest.mark.asyncio
Expand All @@ -137,15 +137,15 @@ async def test_generate_with_str_prompt(setup_test: SetupFixture) -> None:

response = await ai.generate(prompt='hi', config={'temperature': 11})

assert response.text == '[ECHO] user: "hi" {"temperature":11.0}'
assert response.text == '[ECHO] user: "hi" {"temperature":11}'


@pytest.mark.asyncio
async def test_generate_with_part_prompt(setup_test: SetupFixture) -> None:
"""Test that the generate function with a part prompt works."""
ai, *_ = setup_test

want_txt = '[ECHO] user: "hi" {"temperature":11.0}'
want_txt = '[ECHO] user: "hi" {"temperature":11}'

response = await ai.generate(prompt=Part(root=TextPart(text='hi')), config={'temperature': 11})

Expand All @@ -161,7 +161,7 @@ async def test_generate_with_part_list_prompt(setup_test: SetupFixture) -> None:
"""Test that the generate function with a list of parts prompt works."""
ai, *_ = setup_test

want_txt = '[ECHO] user: "hello","world" {"temperature":11.0}'
want_txt = '[ECHO] user: "hello","world" {"temperature":11}'

response = await ai.generate(
prompt=[Part(root=TextPart(text='hello')), Part(root=TextPart(text='world'))],
Expand All @@ -183,7 +183,7 @@ async def test_generate_with_str_system(setup_test: SetupFixture) -> None:
"""Test that the generate function with a string system works."""
ai, *_ = setup_test

want_txt = '[ECHO] system: "talk like pirate" user: "hi" {"temperature":11.0}'
want_txt = '[ECHO] system: "talk like pirate" user: "hi" {"temperature":11}'

response = await ai.generate(system='talk like pirate', prompt='hi', config={'temperature': 11})

Expand All @@ -199,7 +199,7 @@ async def test_generate_with_part_system(setup_test: SetupFixture) -> None:
"""Test that the generate function with a part system works."""
ai, *_ = setup_test

want_txt = '[ECHO] system: "talk like pirate" user: "hi" {"temperature":11.0}'
want_txt = '[ECHO] system: "talk like pirate" user: "hi" {"temperature":11}'

response = await ai.generate(
system=Part(root=TextPart(text='talk like pirate')),
Expand All @@ -223,7 +223,7 @@ async def test_generate_with_part_list_system(setup_test: SetupFixture) -> None:
"""Test that the generate function with a list of parts system works."""
ai, *_ = setup_test

want_txt = '[ECHO] system: "talk","like pirate" user: "hi" {"temperature":11.0}'
want_txt = '[ECHO] system: "talk","like pirate" user: "hi" {"temperature":11}'

response = await ai.generate(
system=[Part(root=TextPart(text='talk')), Part(root=TextPart(text='like pirate'))],
Expand Down Expand Up @@ -257,7 +257,7 @@ async def test_generate_with_messages(setup_test: SetupFixture) -> None:
config={'temperature': 11},
)

assert response.text == '[ECHO] user: "hi" {"temperature":11.0}'
assert response.text == '[ECHO] user: "hi" {"temperature":11}'

_, response = ai.generate_stream(
messages=[
Expand All @@ -269,7 +269,7 @@ async def test_generate_with_messages(setup_test: SetupFixture) -> None:
config={'temperature': 11},
)

assert (await response).text == '[ECHO] user: "hi" {"temperature":11.0}'
assert (await response).text == '[ECHO] user: "hi" {"temperature":11}'


@pytest.mark.asyncio
Expand Down
8 changes: 4 additions & 4 deletions py/plugins/flask/tests/flask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def test_streaming() -> None:
chunks.append(chunk)

assert chunks == [
b'data: {"message": 1}\n\n',
b'data: {"message": {"username": "Pavel"}}\n\n',
b'data: {"message": {"foo": "bar"}}\n\n',
b'data: {"result": {"bar": "baz"}}\n\n',
b'data: {"message":1}\n\n',
b'data: {"message":{"username":"Pavel"}}\n\n',
b'data: {"message":{"foo":"bar"}}\n\n',
b'data: {"result":{"bar":"baz"}}\n\n',
]
Loading