fix(ai): apply input and output schemas when loading .prompt files#300
Conversation
The folder loader read prompt metadata from `parse`, which returns the raw frontmatter and does not resolve Picoschema. As a result: - `input.schema` was never wired onto the prompt action, so the action had a null input schema. The Developer UI shows no input form and input was not validated. - `output.schema` was copied through as raw Picoschema, so an invalid schema was sent to the model as the response schema. Resolve both schemas in the loader. Input schema is converted to a SchemanticType and set on the PromptConfig. Output schema is converted to JSON Schema before building the output config. Conversion does not gate on Picoschema.isPicoschema, which misses the `type, description` form used in the docs; it checks for an existing JSON Schema shape instead. Fixes #299
There was a problem hiding this comment.
Code Review
This pull request adds support for converting input and output schemas defined in Picoschema format within .prompt files into standard JSON Schema. It introduces helper functions to detect existing JSON Schema structures and perform the conversion, ensuring that the Developer UI can render forms and validate inputs correctly. The feedback suggests improving the robustness of the JSON Schema detection in _isJsonSchema by checking for additional keywords like properties or anyOf, and adding a safe type check in _toInputSchema to prevent potential runtime TypeErrors when casting the parsed JSON.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Address review feedback on the .prompt schema loading. - `_isJsonSchema` now also recognizes JSON Schema that omits the top-level `type` and relies on `properties`, `items`, or a `*Of` combinator, so such schemas are not mistaken for Picoschema and re-converted. - The generated input `parse` no longer casts with `as Map`. A prompt can be invoked with null input, and `parse` is called with it, so guard the cast and return an empty map instead of throwing.
|
Addressed both review comments in 7feb3f3 and did another pass over the change. Verified edge cases (added tests for the first two):
One known limitation, out of scope here: a named schema reference is passed through as a raw |
Pass `defineSchema`-registered schemas to Picoschema conversion when loading `.prompt` files, so input and output schemas can reference named schemas by name (e.g. `schema: MyAddress`). Without this, prompts using named schema references could not be resolved.
Fixes #299.
Problem
When a prompt is loaded from a
.promptfile viapromptDir, theinputandoutputschemas in the frontmatter are not applied. The folder loader reads metadata fromdotpromptRegistry.parse(source), which returns the raw frontmatter and does not resolve Picoschema (that only happens in_resolveMetadata, reached throughrender/renderMetadata).As a result:
input.schemawas never set on the prompt action, so the action had a null input schema. The Developer UI renders its input form from that schema, so file-based prompts showed no input fields, and input was never validated.output.schemawas copied through as raw Picoschema. That raw value was then sent to the model as the response schema. With the Google GenAI plugin it becomesresponseJsonSchema, which is not valid JSON Schema, so the request can fail or the schema is ignored.Both schemas already work when a prompt is defined in code with
definePrompt. This was specific to the folder loader.Changes
In
prompt_loader_io.dart:input.schemaand set it on thePromptConfig(and therefore the registeredPromptAction) as aSchemanticType.output.schemato JSON Schema before building the output config._toJsonSchemahelper that converts Picoschema and leaves existing JSON Schema untouched.The conversion does not gate on
Picoschema.isPicoschema. That helper does not recognize thetype, descriptionform (for examplename: string, the person to greet) used throughout the docs, so gating on it would skip the most common case._isJsonSchemachecks for an existing JSON Schema shape (a top-level standardtype, or a$schema/$ref/$defskey) and everything else is treated as Picoschema and converted.Testing
Added three tests to the
loadPromptFoldergroup inprompt_test.dart:requiredset.input.schemapasses through unchanged.Full
dart testfor the genkit package passes (368 tests).dart analyzeis clean.Before, for a prompt with
output.schemaof{category: "string, the predicted category"}:After:
Notes / follow-ups
Picoschema.isPicoschemanot matching thetype, descriptionform looks like a bug in thedotpromptpackage itself. This PR works around it in the loader. It may be worth fixing upstream too.input.defaultare still not applied at render time. That is a separate render-path change and is out of scope here.