fix: resolve omitted Optional inputs to None at the HTTP edge#3051
Merged
Conversation
A predictor input declared `Optional[T]` / `T | None` is emitted as an optional field in the OpenAPI schema (nullable, absent from `required`), but whether it can actually be omitted at request time depended on a Python-level detail: whether the parameter had a usable `__defaults__` entry. A bare `value: Optional[str]` (no `= Input(...)`) built and served fine, advertised the field as optional, then raised `TypeError: missing 1 required positional argument` whenever a caller omitted it. Fix this in coglet's HTTP edge, where input validation already runs with the OpenAPI schema in hand. After validation passes, inject an explicit `null` for every property that is `nullable: true`, absent from `required`, and has no `default` key. This mirrors the schema-generation discriminator in pkg/schema/openapi.go and keeps the schema the source of truth for what is optional. The worker is unchanged and needs no schema. The rule safely skips `Optional[T] = Input(default=None)` (emits `default: null`), real defaults, and required fields. Injection runs only after successful validation, so missing required fields still 422. Training inputs (TrainingInput) are covered via the same path. Union optionals remain in `required` by design and are correctly skipped. Tests: - unit (input_validation.rs): inject for bare optional, optional list, optional enum; skip explicit-default and required fields; never override a present value. - unit (service.rs): predict injects after Ok, does not inject when a required field is missing, train path injects. - integration: optional_input_no_default.txtar exercises a bare `value: Optional[str]` via cog predict and POST /predictions.
Contributor
|
LGTM |
JoaquinGimenez1
approved these changes
Jun 8, 2026
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.
Problem
A predictor input declared
Optional[T]/T | Noneis emitted as an optional field in the OpenAPI schema (nullable: true, absent fromrequired), but whether it can actually be omitted at request time depended on a Python-level detail the schema doesn't capture: whether the parameter had a usable__defaults__entry.value: Optional[str](bare, noInput)TypeError: missing 1 required positional argumentvalue: Optional[str] = Input(description="…")Nonevalue: Optional[str] = Input(default=None)NoneAll three generate a correct "optional" schema. The bare form was a silent footgun: it built and served fine, advertised the field as optional, then failed at prediction time whenever a caller omitted it.
Fix
Validation against the OpenAPI schema already runs at coglet's HTTP edge (
InputValidator), not in the worker. After validation passes, inject an explicitnullfor every property that isnullable: true, not inrequired, and has nodefaultkey, when it is absent from the request.This rule mirrors the schema-generation discriminator in
pkg/schema/openapi.go(buildInputSchema), keeps the OpenAPI schema as the source of truth for what is optional, and leaves the worker unchanged (it needs no schema).The rule safely skips:
Optional[T] = Input(default=None)— emitsdefault: null, already handled byunwrap_field_info_defaultsInput(default=42)— emitsdefault: 42Injection runs only after successful validation, so missing required fields still return 422. Training inputs (
TrainingInput) are covered via the same path. Union optionals (A | B | None) are forced intorequiredby the generator and are correctly skipped (they 422 on omission rather than resolving toNone— pre-existing behavior, not a regression).Tests
input_validation.rs): inject for bare optional / optional list / optional enum; skip explicit-default and required fields; never override a present value.service.rs): predict injects afterOk, does not inject when a required field is missing, train path injects.optional_input_no_default.txtar): a barevalue: Optional[str]omitted via bothcog predictandPOST /predictions {"input":{}}resolves toNone.