Skip to content

test(py): pin EmbedRequest[Any] object schema for untyped embedder options - #6984

Merged
huangjeff5 merged 2 commits into
mainfrom
fix/py-untyped-options-schema-guard
Nov 3, 2026
Merged

huangjeff5 merged 2 commits into
mainfrom
fix/py-untyped-options-schema-guard

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Pins the JSON Schema contract for untyped embedder options (EmbedRequest[Any]), ensuring plugin embedders that don't declare a config model still advertise options as a JSON object ({"type": "object", "additionalProperties": true}) to the Dev UI and reflection API rather than an unconstrained any schema.

from typing import Any
from genkit.embedder import Embedder, EmbedRequest, EmbedResponse
from genkit.plugin_api import to_json_schema


# Plugin author defines an embedder without a custom Pydantic config class:
async def embed_menu_items(request: EmbedRequest[Any]) -> EmbedResponse:
    # At runtime, EmbedRequest validates `request.options` as a dict | None
    dims = (request.options or {}).get('dimensions', 1024)
    return EmbedResponse(embeddings=[...])


# --- WITHOUT _untyped_options_schema (previously untested on main) ---
# Bare `EmbedRequest` got `'type': 'object'` from RequestOptionsT's `dict[str, Any]` default,
# but `EmbedRequest[Any]` substituted `Any` and emitted an unconstrained schema with no `type`:
to_json_schema(EmbedRequest[Any])['properties']['options']
# => {'title': 'Options'}
#    Dev UI / reflection schema says any JSON scalar ('hello', 42) is valid for `options`,
#    even though runtime validation rejects non-dicts with ValidationError.

# --- WITH _untyped_options_schema (now pinned in validation & serialization modes) ---
to_json_schema(EmbedRequest[Any])['properties']['options']
# => {'additionalProperties': True, 'title': 'Options', 'type': 'object'}
#    Matches bare `EmbedRequest` and runtime dict validation in both schema modes.

Context & Main Changes

  • Why this PR exists: feat(py)!: one config class per action; typed options for embedders #6714 added _untyped_options_schema on EmbedRequest / ModelRequest so EmbedRequest[Any] advertises 'type': 'object' instead of any. This PR originally started as a defensive schema.setdefault('properties', {}) guard on that hook, but review (@hilariie, @cabljac) confirmed Pydantic's GenerateJsonSchema always populates 'properties' on BaseModel schemas—and uncovered that deleting _untyped_options_schema altogether left the entire test suite green because nothing tested EmbedRequest[Any] schema output.
  • Kept fail-fast schema['properties'] lookup: Reverted setdefault back to schema['properties'] so any broken custom schema generator raises KeyError loudly instead of emitting a malformed object schema, and updated _untyped_options_schema's docstring to name EmbedRequest[Any] explicitly.
  • Pinned EmbedRequest and EmbedRequest[Any] across both Pydantic schema modes: Added TypeAdapter(...).json_schema(mode=...) assertions over EmbedRequest and EmbedRequest[Any] in both 'validation' and 'serialization' modes, plus declared_config_type(EmbedRequest[Any]) is None (follow-up subclass MRO tracking filed in fix(py): declared_config_type misses generic args on subclasses of EmbedRequest[T] and ModelRequest[T] genkit-python#201).

@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 updates the _untyped_options_schema function in _model.py to safely handle cases where the schema dictionary does not contain a 'properties' key by using setdefault('properties', {}). Additionally, a unit test has been added to verify this behavior and ensure missing properties are correctly populated. No review comments were provided, so there is no feedback to address.

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

Is there a path where pydantic actually calls this hook without properties? Every model schema I tried (both modes, generics, subclasses, nested) already had it, so this reads as hardening rather than a KeyError fix.

@huangjeff5
huangjeff5 force-pushed the fix/py-untyped-options-schema-guard branch from 1a6bb3f to fe02b6e Compare November 2, 2026 23:04
@huangjeff5 huangjeff5 changed the title fix(py): use setdefault for properties in _untyped_options_schema chore(py): harden _untyped_options_schema and cover EmbedRequest[Any] schema Nov 2, 2026
@huangjeff5

Copy link
Copy Markdown
Contributor Author

checked GenerateJsonSchema.model_schema -> model_fields_schema -> _named_required_fields_schema: Pydantic unconditionally initializes {'type': 'object', 'properties': properties} for every non-RootModel BaseModel (even with zero fields), and @model_serializer short-circuits before _update_class_schema. only a RootModel or a custom GenerateJsonSchema subclass overriding model_fields_schema can reach json_schema_extra without 'properties', so you're right — this is hardening rather than a reachable KeyError fix.

reframed the PR title/body to chore(py) and swapped the test to TypeAdapter(EmbedRequest[Any]).json_schema() in fe02b6e.

@huangjeff5
huangjeff5 requested a review from hilariie November 2, 2026 23:04

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

LGTM with comments

@huangjeff5 huangjeff5 changed the title chore(py): harden _untyped_options_schema and cover EmbedRequest[Any] schema test(py): cover EmbedRequest[Any] schema in validation and serialization modes Nov 3, 2026
@huangjeff5 huangjeff5 changed the title test(py): cover EmbedRequest[Any] schema in validation and serialization modes test(py): pin EmbedRequest[Any] object schema for untyped embedder options Nov 3, 2026
@huangjeff5
huangjeff5 enabled auto-merge (squash) November 3, 2026 18:08
@huangjeff5
huangjeff5 merged commit 09dd85f into main Nov 3, 2026
37 of 38 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-untyped-options-schema-guard branch November 3, 2026 18:46
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.

3 participants