feat: model input validation before build and run#3097
Open
anish-sahoo wants to merge 14 commits into
Open
Conversation
Validate CLI inputs for `cog predict`, `cog run`, and `cog train` against the model's OpenAPI schema before the expensive build/pull/start steps, so bad inputs fail fast instead of after a full Docker build or container start. - Add pkg/predict/input_validation.go with schema-driven validation that reports friendly errors (unknown/missing inputs, type mismatches, enum values, numeric constraints). - Extract static schema generation into pkg/schema/static so both the image build and CLI preflight share one code path. - predict/train: generate the schema up front from cog.yaml (local source) or the image's openapi_schema label (existing image) and validate before building/starting. Images without the label fall back to the runtime schema fetched after start, preserving prior behavior. - Surface file-read failures with the offending input name. - Add unit and integration tests covering the new validation paths and pin user-facing error wording to guard against kin-openapi changes.
Rename the config-aware schema generator package to pkg/schema/openapi so call sites read openapi.Generate(cfg, dir). Rename the existing renderer file to openapi_spec.go to free the openapi name.
Rename the entry point to openapi.GenerateSchema so it's clear what it produces. In generateLocalOpenAPISchema, rename the loaded document to `spec` (was shadowing `schema`) and the raw bytes to `openapiSchema` (avoids implying JSON Schema).
markphelps
reviewed
Jul 9, 2026
Address review feedback: use a single var block for the local declarations in cmdPredict/cmdTrain instead of mixing var and := syntax.
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
Address PR review feedback on input preflight validation: - Coerce '-i name=true' to a boolean and repeated/single '-i name=v' array values to the array's item type, so booleans and numeric/bool arrays reach the runtime with the intended type and pass preflight validation instead of failing as strings. - Preserve typed array elements through Inputs.toMap (previously arrays were always flattened to []string). - Only run preflight validation for existing images when the schema label actually carries the Input/TrainingInput component; otherwise fall back to the runtime schema (guards minimal/malformed labels). - Include the input name in JSON-mode file read errors for parity with the -i path. - Add integration coverage for omitting a required input (local source before build, and existing image before start).
Address remaining PR review feedback: - #7: local-source 'cog predict'/'cog train' now generate the OpenAPI schema once for input preflight and pass the bytes into the build via BuildOptions.OpenAPISchema, so the build reuses them instead of regenerating. This removes the second generation and the chance of drift between the validated schema and the image label. Extracted the schema-selection logic into resolveBuildSchema for unit coverage. - #8: the runtime validates inputs as strict JSON Schema and ignores the OpenAPI 'nullable' keyword, so it rejects an explicit null for every field. kin-openapi honors 'nullable' and would accept it, letting a '--json' request pass preflight only to 422 at runtime. Reject explicit nulls in preflight so it matches runtime. (The other cases the reviewer flagged -- numeric strings and Any/object inputs -- were verified to already match the strict runtime.) Documented the one intentional divergence: unknown inputs are rejected at the CLI rather than stripped as the runtime does.
Rename schemaJSON/schema to openAPISchemaJSON/openAPISchema in the cmdPredict/cmdTrain local-source paths to make the raw-bytes vs parsed distinction explicit and match the BuildOptions.OpenAPISchema field.
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
markphelps
reviewed
Jul 9, 2026
Address adversarial review feedback:
- Add tests for the previously untested null-composition branches of
schemaAllowsNullWithoutNullable (oneOf exactly-one, enum null
containment, not) plus oneOf null rejection/acceptance through the
public ValidateInputMapForMode entry point, protecting preflight vs
runtime parity.
- Dedup the two identical quoting loops and four-way message branch in
validateKnownInputs into a quoteJoin helper.
- Harden missingRequiredInput to key off schemaErr.SchemaField
('required') before parsing the free-text reason.
- Document validation ownership across the three entry points and add a
defensive nil guard in NewInputsForMode.
01701f1 to
84d802d
Compare
Contributor
There was a problem hiding this comment.
I'm Bonk, and I've done a quick review of your PR.
This PR adds preflight input validation for cog predict, cog run, and cog train against the OpenAPI schema before expensive build/start steps. It also extracts the schema generation into a reusable pkg/schema/openapi package and improves CLI input coercion for booleans and numeric arrays. Overall the changes are well-structured and thoroughly tested.
One minor issue in changed code:
Contributor
|
Review submitted to PR #3097. I found one minor issue where |
Contributor
|
LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Validate
cog predict,cog run, andcog traininputs against the model's OpenAPI schema before building the image or starting the container. Bad inputs fail immediately instead of after a full build or container start.cog.yamlin Go — no Docker or Python needed.openapi_schemalabel after pull, before start.expected integer, got string.nullfor typed fields, matching runtime semantics.-istring values to schema types before validating.pkg/schema/openapi.GenerateSchema, one config-aware entry point shared by build and CLI preflight.pkg/schema/openapi.go→openapi_spec.go.Notes: