Skip to content

feat(py): support ModelRef in ai.generate() and ai.define_prompt() - #5966

Closed
huangjeff5 wants to merge 33 commits into
genkit-ai:feat/py-generic-model-reffrom
huangjeff5:feat/py-core-model-ref-generate
Closed

huangjeff5 wants to merge 33 commits into
genkit-ai:feat/py-generic-model-reffrom
huangjeff5:feat/py-core-model-ref-generate

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

Summary

Enables passing typed ModelRef instances directly to ai.generate(), ai.generate_stream(), ai.generate_operation(), and ai.define_prompt().

Key Changes

  • resolve_model_arg() helper: Automatically extracts the wire model name (model.name), default configuration (model.config), and version from a ModelRef. Merges call-time configuration overrides on top of the ModelRef's default configuration and validates against its concrete Pydantic schema.
  • Veneer API Support: Updated Genkit.generate(), Genkit.generate_stream(), Genkit.generate_operation(), and Genkit.define_prompt() signatures to accept model: str | ModelRef[BaseModel] | None.
  • Unit Tests: Added model_ref_generate_test.py and model_ref_test.py covering end-to-end generation with ModelRef and config override merging behavior.

Builds on #5965.

@github-actions github-actions Bot added the python Python label Aug 6, 2026
@huangjeff5
huangjeff5 changed the base branch from main to feat/py-generic-model-ref August 6, 2026 02:05

@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 introduces support for typed and frozen ModelRef objects across the generation and prompt APIs, allowing model references to carry default configurations that are validated and merged with call-time overrides. Feedback on the changes suggests removing redundant unit tests for ModelRef from model_test.py since the same behavior is already covered in the newly created model_ref_test.py file.

I am having trouble creating individual review comments. Click here to see my feedback.

py/packages/genkit/tests/genkit/ai/model_test.py (381-416)

medium

These tests for ModelRef seem redundant, as similar, more specific tests are added in the new py/packages/genkit/tests/genkit/ai/model_ref_test.py file. To avoid test duplication and keep tests organized, it would be better to have all ModelRef unit tests in model_ref_test.py.

Consider removing test_model_ref_generic_construction and test_model_ref_immutability from this file. The functionality is already covered by test_model_ref_stamps_namespace_and_schema, test_model_ref_stamps_typed_config, and test_model_ref_is_frozen in model_ref_test.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant