-
Notifications
You must be signed in to change notification settings - Fork 695
feat: model input validation before build and run #3097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anish-sahoo
wants to merge
17
commits into
main
Choose a base branch
from
io-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b987ac9
feat: validate model inputs before build and run
anish-sahoo c5b6b02
refactor: make openapi.Generate the config-aware schema entry point
anish-sahoo db4fa18
refactor: rename openapi.Generate to GenerateSchema and clarify locals
anish-sahoo bf16d88
style: consolidate cmd declarations into var blocks
anish-sahoo c73635c
fix: coerce CLI inputs to schema types and refine preflight fallback
anish-sahoo 327b15d
refactor: generate local schema once; match runtime null semantics
anish-sahoo 4fdbf73
refactor: rename local schema vars for clarity
anish-sahoo 7153eed
fix: align preflight validation with runtime schemas
anish-sahoo b628392
refactor: simplify input coercion
anish-sahoo eb8cd64
test: cover oneOf/enum/not null branches; harden and dedup validation
anish-sahoo 84d802d
Clean up comments
anish-sahoo 64209b7
Document defensive walker
anish-sahoo 2b66d35
consolidate integration tests
anish-sahoo 2618c48
fix: return nil instead of partial map on toMap error
anish-sahoo 3c7bdf7
Merge branch 'main' into io-validation
anish-sahoo a9fa53f
Merge branch 'main' into io-validation
anish-sahoo 7f82a61
fix: handle incomplete input schemas safely
anish-sahoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
102 changes: 102 additions & 0 deletions
102
integration-tests/tests/input_validation_before_build.txtar
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Local-source inputs are validated against the statically-generated schema | ||
| # *before* the Docker image is built. Every command below must fail at preflight | ||
| # with a clear message and must not start a build. Covers run, the deprecated | ||
| # predict alias, and train, across -i and --json, for the full range of | ||
| # validation errors (unknown/missing/type/range/length/regex/enum/null). | ||
|
|
||
| # --- Unknown input name (-i and --json) --- | ||
| ! cog run -i prompt=hi -i nope=1 | ||
| stderr 'unknown input "nope"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| ! cog run --json '{"prompt": "hi", "nope": 1}' | ||
| stderr 'unknown input "nope"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Missing a required input --- | ||
| ! cog run | ||
| stderr 'missing required input "prompt"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Numeric bounds: ge (0 < 1) and le (99 > 10) --- | ||
| ! cog run -i prompt=hi -i count=0 | ||
| stderr 'invalid input "count"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| ! cog run -i prompt=hi -i count=99 | ||
| stderr 'invalid input "count"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Type mismatch (not an integer) --- | ||
| ! cog run -i prompt=hi -i count=lots | ||
| stderr 'invalid input "count": expected integer, got string' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Choices / enum --- | ||
| ! cog run -i prompt=hi -i size=huge | ||
| stderr 'invalid input "size": must be one of: small, medium, large' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- String length (max_length=5) --- | ||
| ! cog run -i prompt=hi -i title=toolong | ||
| stderr 'invalid input "title"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Regex (must match ^[a-z]+$) --- | ||
| ! cog run -i prompt=hi -i slug=BAD | ||
| stderr 'invalid input "slug"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Explicit null is rejected, even for an optional/nullable field --- | ||
| ! cog run --json '{"prompt": "hi", "count": null}' | ||
| stderr 'must not be null' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| ! cog run --json '{"prompt": "hi", "note": null}' | ||
| stderr 'must not be null' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- The deprecated `cog predict` local path validates the same way --- | ||
| ! cog predict -i prompt=hi -i count=0 | ||
| stderr 'invalid input "count"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| # --- Training inputs are validated before build too --- | ||
| ! cog train -i epochs=0 | ||
| stderr 'invalid input "epochs"' | ||
| ! stderr 'Building Docker image' | ||
|
|
||
| -- cog.yaml -- | ||
| build: | ||
| python_version: "3.12" | ||
| predict: "predict.py:Predictor" | ||
| train: "train.py:train" | ||
|
|
||
| -- predict.py -- | ||
| from typing import Optional | ||
|
|
||
| from cog import BasePredictor, Input | ||
|
|
||
|
|
||
| class Predictor(BasePredictor): | ||
| def predict( | ||
| self, | ||
| prompt: str, | ||
| count: int = Input(default=5, ge=1, le=10), | ||
| size: str = Input(default="small", choices=["small", "medium", "large"]), | ||
| title: str = Input(default="ok", max_length=5), | ||
| slug: str = Input(default="ok", regex="^[a-z]+$"), | ||
| note: Optional[str] = Input(default=None), | ||
| ) -> str: | ||
| return prompt | ||
|
|
||
| -- train.py -- | ||
| from cog import BaseModel, Input | ||
|
|
||
|
|
||
| class TrainingOutput(BaseModel): | ||
| weights: str | ||
|
|
||
|
|
||
| def train(epochs: int = Input(ge=1)) -> TrainingOutput: | ||
| return TrainingOutput(weights="ok") |
32 changes: 32 additions & 0 deletions
32
integration-tests/tests/input_validation_before_start.txtar
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Inputs to a pre-built image are validated against the image's embedded schema | ||
| # *before* the container starts. Builds once, then exercises the validation | ||
| # paths (unknown/missing) via both -i and --json. None may start the container. | ||
|
|
||
| cog build -t $TEST_IMAGE | ||
|
|
||
| # --- Unknown input name (-i and --json) --- | ||
| ! cog predict $TEST_IMAGE -i nope=value | ||
| stderr 'unknown input "nope"' | ||
| ! stderr 'Starting Docker image and running setup' | ||
|
|
||
| ! cog predict $TEST_IMAGE --json '{"nope": 1}' | ||
| stderr 'unknown input "nope"' | ||
| ! stderr 'Starting Docker image and running setup' | ||
|
|
||
| # --- Missing a required input --- | ||
| ! cog predict $TEST_IMAGE | ||
| stderr 'missing required input "prompt"' | ||
| ! stderr 'Starting Docker image and running setup' | ||
|
|
||
| -- cog.yaml -- | ||
| build: | ||
| python_version: "3.12" | ||
| predict: "predict.py:Predictor" | ||
|
|
||
| -- predict.py -- | ||
| from cog import BasePredictor | ||
|
|
||
|
|
||
| class Predictor(BasePredictor): | ||
| def predict(self, prompt: str) -> str: | ||
| return prompt |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.