Skip to content

fix(py): a model's config form uses ModelInfo.config_schema - #6641

Closed
huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-model-config-form-custom-options
Closed

huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-model-config-form-custom-options

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

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 validates config=; a schema on ModelInfo draws 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 customOptions comes from

The Dev UI draws a model's config form from one place: metadata.model.customOptions on the action, a JSON Schema. Every SDK writes it at define time, and the reflection server serves it on /api/actions:

# 1. Plugin defines a model with a config class
class SketchConfig(BaseModel):
    quality: Literal['low', 'high'] = 'low'

ai.define_model('acme/sketch', generate_sketch, config_schema=SketchConfig)

# 2. Genkit turns the class into JSON Schema and stores it on the action
#    (JS does the same: defineModel({configSchema}) -> metadata.model.customOptions)
#
# 3. The Dev UI fetches GET /api/actions:
# => "/model/acme/sketch": {
#      "metadata": {
#        "model": {
#          "label": "acme/sketch",
#          "customOptions": {
#            "type": "object",
#            "properties": {"quality": {"enum": ["low", "high"], ...}}
#          }
#        }
#      }
#    }
#
# 4. model-config.component.ts reads metadata.model.customOptions.properties and draws
#    a "quality" dropdown. The value you pick comes back on the run as config={'quality': 'high'}.

ModelInfo also has a config_schema field (configSchema on the wire, from the shared genkit-schema ModelInfo). The Dev UI never reads that key. Only customOptions drives the form.

What was broken

A plugin whose config is JSON-only (no Python class) describes it on ModelInfo. Python copied it into metadata as configSchema, so the Dev UI got nothing:

QUALITY = {'type': 'object', 'properties': {'quality': {'type': 'string', 'enum': ['low', 'high']}}}

ai.define_model('acme/sketch', generate_sketch, info=ModelInfo(config_schema=QUALITY))

# before
# metadata['model'] == {'label': 'acme/sketch', 'configSchema': QUALITY}
# => Dev UI: no customOptions, empty config form

# after
# metadata['model'] == {'label': 'acme/sketch', 'customOptions': QUALITY}
# => Dev UI: "quality" dropdown

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

# 1. Class only: unchanged
ai.define_model('acme/sketch', fn, config_schema=SketchConfig)
# => customOptions == to_json_schema(SketchConfig)

# 2. Info schema only: fixed by this PR
ai.define_model('acme/sketch', fn, info=ModelInfo(config_schema=QUALITY))
# => customOptions == QUALITY

# 3. Both: config_schema= wins
ai.define_model('acme/sketch', fn, config_schema=SketchConfig, info=ModelInfo(config_schema=QUALITY))
# => customOptions == to_json_schema(SketchConfig)

configSchema is no longer emitted in any case. define_background_model follows the same three rules. define_model also moves a configSchema passed through metadata={'model': {...}} to customOptions.

model_action_metadata (used by plugins in list_actions) also takes a ModelInfo now. Before, it only took a dict:

# before
model_action_metadata('acme/sketch', info=ModelInfo(config_schema=QUALITY))
# => TypeError: 'ModelInfo' object is not a mapping

# after
model_action_metadata('acme/sketch', info=ModelInfo(config_schema=QUALITY)).metadata['model']
# => {'label': 'acme/sketch', 'customOptions': QUALITY}

So a model shows the same form whether it comes from list_actions or is resolved and defined.

Decisions

  • One config-schema key, customOptions: The Dev UI and JS already use customOptions and ignore configSchema. 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: When config_schema= and info.config_schema are both set, customOptions comes from config_schema=, whether it's a class or a dict. A class there also validates config= on the call, so the form and the validator can't disagree.

@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 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.

Comment on lines +74 to +75
if key not in options:
options[key] = value

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.

medium

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:

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.

medium

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.

Suggested change
if config_schema:
if config_schema is not None:

@huangjeff5

huangjeff5 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing this. config_schema= stays the only way to give a model a config form.

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 config_schema=:

ai.define_model('acme/sketch', generate_sketch, config_schema=QUALITY)
# => metadata['model']['customOptions'] == QUALITY  (Dev UI draws the form)

Why not also read ModelInfo.config_schema

Python is the only SDK where a model has two places to put a config schema:

  • JS: defineModel({configSchema}) is the only source for customOptions. DefineModelOptions is a flat options type, not the generated ModelInfo.
  • Go: ModelOptions.ConfigSchema is the only explicit source. When it's nil, the schema is inferred from the Config type, and the same schema validates config on every call.
  • Python: define_model(info=...) takes the generated ModelInfo, so it inherits that type's config_schema field alongside the config_schema= kwarg.

Nothing in any SDK or the Dev UI reads ModelInfo.configSchema today. Making Python read it would turn an unused field into a second source and force a precedence rule between two explicit schemas. Neither JS nor Go has that rule, because neither has a second source.

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 info.config_schema is set. That adds an error path, plus tests and docs, for a field no first-party plugin uses. Leaving it unused is simpler.

State after closing

@huangjeff5 huangjeff5 closed this Oct 8, 2026
@huangjeff5
huangjeff5 deleted the fix/py-model-config-form-custom-options branch October 8, 2026 18:07
@huangjeff5
huangjeff5 restored the fix/py-model-config-form-custom-options branch October 8, 2026 18:07
@huangjeff5
huangjeff5 removed this pull request from stack #6676 October 8, 2026 18:08
@huangjeff5
huangjeff5 deleted the fix/py-model-config-form-custom-options branch October 8, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant