Repository navigation
fix(py): a model's config form uses ModelInfo.config_schema - #6641
huangjeff5 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors how model configuration schemas are handled and mapped to customOptions in model metadata. It introduces helper functions absorb_model_info and apply_config_class to standardize this behavior across model actions, background models, and OpenAI plugins, accompanied by comprehensive test coverage. The review feedback highlights two important improvements: ensuring consistent key-overwriting behavior in absorb_model_info when handling Mapping versus ModelInfo objects, and changing the truthiness check of config_schema to an explicit is not None check to correctly support empty dictionary schemas.
| if key not in options: | ||
| options[key] = value |
There was a problem hiding this comment.
In absorb_model_info, when info is a Mapping, keys are only copied if they are not already in options (if key not in options:). However, when info is a ModelInfo object, options.update is used, which unconditionally overwrites existing keys. To ensure consistent behavior and prevent subtle bugs if options is pre-populated, we should unconditionally copy the keys for mappings as well.
options[key] = value|
|
||
| def apply_config_class(*, options: dict[str, object], config_schema: type | dict[str, Any] | None) -> None: | ||
| """A config class also checks ``config=`` on the call, so it wins the form.""" | ||
| if config_schema: |
There was a problem hiding this comment.
Using if config_schema: will evaluate to False if config_schema is an empty dictionary {}. Since an empty dictionary is a valid JSON schema, we should explicitly check if config_schema is not None: to ensure that empty schemas are correctly processed and applied.
| if config_schema: | |
| if config_schema is not None: |
7567b31 to
642e9f5
Compare
|
Closing this. Why this isn't needed The case this PR fixed was a plugin whose config is JSON-only, with no Python class. That plugin can already pass the dict straight to ai.define_model('acme/sketch', generate_sketch, config_schema=QUALITY)
# => metadata['model']['customOptions'] == QUALITY (Dev UI draws the form)Why not also read Python is the only SDK where a model has two places to put a config schema:
Nothing in any SDK or the Dev UI reads Go doesn't support "info wins" either: there, an explicit schema beats an inferred one. It never beats another explicit source. I also considered raising when both are set, or whenever State after closing
|
A model defined with
ModelInfo(config_schema=...)and no config class now shows that schema in the Dev UI config form. Before this PR the form was empty.This is provider-facing: it changes what a model advertises, not how it runs.
ai.generate(..., config=...)behaves the same. Only a config class validatesconfig=; a schema onModelInfodraws the form, and the model function receives the caller's dict as-is. First-party plugins already pass a config class, so their forms don't change.Where
customOptionscomes fromThe Dev UI draws a model's config form from one place:
metadata.model.customOptionson the action, a JSON Schema. Every SDK writes it at define time, and the reflection server serves it on/api/actions:ModelInfoalso has aconfig_schemafield (configSchemaon the wire, from the shared genkit-schemaModelInfo). The Dev UI never reads that key. OnlycustomOptionsdrives the form.What was broken
A plugin whose config is JSON-only (no Python class) describes it on
ModelInfo. Python copied it into metadata asconfigSchema, so the Dev UI got nothing:Plugins worked around this by stripping the schema off the info and passing a class instead. OpenAI does
model_info.model_dump(..., exclude={'config_schema'})and passes the class separately.All three cases
configSchemais no longer emitted in any case.define_background_modelfollows the same three rules.define_modelalso moves aconfigSchemapassed throughmetadata={'model': {...}}tocustomOptions.model_action_metadata(used by plugins inlist_actions) also takes aModelInfonow. Before, it only took a dict:So a model shows the same form whether it comes from
list_actionsor is resolved and defined.Decisions
customOptions: The Dev UI and JS already usecustomOptionsand ignoreconfigSchema. Emitting both would leave a dead key that looks like it should do something. We chose to publish the info's schema over rejecting it, because plugins with JSON-only config have no other way to describe it.config_schema=wins when both are given: Whenconfig_schema=andinfo.config_schemaare both set,customOptionscomes fromconfig_schema=, whether it's a class or a dict. A class there also validatesconfig=on the call, so the form and the validator can't disagree.