Skip to content

feat(go): add DefineSchemasFor and drop core.DefineSchemaFor - #5885

Open
apascal07 wants to merge 3 commits into
mainfrom
ap/go-define-schemas-for
Open

feat(go): add DefineSchemasFor and drop core.DefineSchemaFor#5885
apascal07 wants to merge 3 commits into
mainfrom
ap/go-define-schemas-for

Conversation

@apascal07

Copy link
Copy Markdown
Collaborator

Ports DefineSchemasFor from the Go V2 branch (#5814). Registering the schemas a .prompt-using app needs took one DefineSchemaFor[T] statement per type, since a type parameter cannot be variadic. DefineSchemasFor takes values instead, so one call covers as many types as the app has.

Before: one statement per type.

genkit.DefineSchemaFor[JokeRequest](g)
genkit.DefineSchemaFor[Joke](g)
genkit.DefineSchemaFor[RecipeRequest](g)
genkit.DefineSchemaFor[Recipe](g)
genkit.DefineSchemaFor[AssistantRequest](g)

After: one call. The registry name and the schema are still inferred from each value's Go type, so .prompt frontmatter referencing schema: Joke resolves exactly as before.

genkit.DefineSchemasFor(g, JokeRequest{}, Joke{}, RecipeRequest{}, Recipe{}, AssistantRequest{})

core.DefineSchemaFor is deleted, genkit.DefineSchemaFor stays

core is plugin and internal plumbing, so its copy goes rather than becoming a shim, following ai.DefineFormat in #5861. genkit.DefineSchemaFor is the app-facing touchpoint and is not deprecated: for a single type, naming it still reads better than constructing a value of it. It now delegates to core.DefineSchemasFor, so the two forms cannot drift.

Taking a value rather than a type parameter also lets registration reject input that never produced a usable schema. DefineSchemaFor[map[string]any] and DefineSchemaFor[struct{ ... }] registered under the empty name, and DefineSchemaFor[SomeInterface] dereferenced a nil reflect.Type. All three now panic with a message pointing at DefineSchema(name, schema), which is the call that can name a schema explicitly. That is the only behavior change to the kept function.

Scope

  • DefineSchema(g, name, schema) is untouched. A raw JSON schema map has no Go type to take a name from, which is why passing one to DefineSchemasFor panics instead of guessing.
  • No registry or .prompt change. Schemas land in the same registry under the same names; only the number of statements it takes to get them there changes.

Testing

Go 1.26.3, full go test ./... in go/: 40 packages pass, no failures. go/samples builds and vets clean.

core_test.go covers multi-value registration, pointer values, and each of the three panic paths. genkit_test.go keeps DefineSchemaFor coverage for both value and pointer type arguments, pinning it to the same schemas DefineSchemasFor produces.

…SchemasFor

DefineSchemaFor took one type parameter per call, so registering the
handful of schemas a .prompt-using app needs meant one statement each.
DefineSchemasFor takes values instead of a type parameter and registers
any number of them in a single call.

Taking values also lets it reject inputs the type-parameter form could
not: a map now panics pointing at DefineSchema(name, schema) instead of
being registered as a schema named "", and nil or an unnamed type panics
rather than dereferencing a nil reflect.Type.

core is plugin and internal plumbing, so the old function is removed
outright rather than kept as a shim. genkit.DefineSchemaFor, the
app-facing touchpoint, is unaffected and stays.
Registers any number of Go-type schemas in one call:

    genkit.DefineSchemasFor(g, JokeRequest{}, Joke{}, Recipe{})

genkit.DefineSchemaFor stays as the single-type form for when naming the
type reads better than constructing a value of it, and now delegates to
core.DefineSchemasFor so both paths register identical schemas.
basic-prompts collapses five DefineSchemaFor statements into one call.
@github-actions github-actions Bot added docs Improvements or additions to documentation go labels Aug 1, 2026

@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 DefineSchemasFor, a new function that allows registering multiple JSON schemas derived from Go types in a single call. It refactors existing code, documentation, and samples to use this new variadic function instead of multiple sequential calls to DefineSchemaFor. Additionally, comprehensive unit tests have been added to verify its behavior, including panic conditions for invalid inputs like maps, unnamed types, and nil values. There are no review comments, so I have no additional feedback to provide.

@apascal07
apascal07 requested review from a team, huangjeff5 and pavelgj August 1, 2026 17:34
@apascal07
apascal07 marked this pull request as ready for review August 1, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation go

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant