Repository navigation
fix(serve): invalid max_error and set bounds answer 422 before inference, with a code - #62
Conversation
Reviewer's GuideThe PR moves invalid max_error and threshold set-bound failures into request parsing/validation so inference is not started, and standardizes all failed HTTP responses with machine-readable codes while adding body-level detail for 422 errors; successful responses and the existing error message remain unchanged. Sequence diagram for pre-inference request validation and coded errorssequenceDiagram
participant Client
participant Server as HTTP server
participant Validation as Request validation
participant Model as Model inference
Client->>Server: POST /v1/systemone or /v1/basal
Server->>Validation: act_spec()
Validation->>Validation: float(max_error)
Validation->>Validation: validate(min, max)
alt Invalid max_error or set bound
Validation-->>Server: RequestError or ValueError
Server-->>Client: 422 {error, code: INVALID_REQUEST, detail}
else Valid request
Validation-->>Server: Validated request
Server->>Model: Run inference
Model-->>Server: Result or failure
alt Out of memory
Server-->>Client: 503 {error, code: OUT_OF_MEMORY}
else Other server error
Server-->>Client: 500 {error, code: INTERNAL_ERROR}
end
end
Flow diagram for standardized HTTP failure responsesflowchart TD
Failure[Request or server failure] --> Status{HTTP status}
Status -->|422| Invalid[INVALID_REQUEST<br/>error + detail]
Status -->|503| Memory[OUT_OF_MEMORY<br/>error]
Status -->|500| Internal[INTERNAL_ERROR<br/>error]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="basal/server.py" line_range="287" />
<code_context>
+ max_error = q.get("max_error")
+ if max_error is not None:
+ try:
+ max_error = float(max_error)
+ except (TypeError, ValueError, OverflowError):
+ raise RequestError(f"question {name!r}: max_error must be a number") from None
</code_context>
<issue_to_address>
**Invalid targets reach inference**
When an `act` request supplies a boolean, non-finite value, or numeric `max_error` outside `(0, 1)`, `float(max_error)` accepts these values, so `act_spec` passes them to inference; `certified` finds no matching threshold, causing a defer action to be selected or a `RequestError`/422 only after inference.
Reject boolean, non-finite, and out-of-range `max_error` values in `act_spec` before inference.
</issue_to_address>
### Comment 2
<location path="docs/API.md" line_range="84-85" />
<code_context>
- **503**: the GPU ran out of memory (CUDA, MPS or Metal). The server frees the failed request's memory, so a retry,
later or on another server, may succeed;
- **500**: any other server error.
+ Every error body also has a `code`: `INVALID_REQUEST` (422), `OUT_OF_MEMORY` (503) or `INTERNAL_ERROR` (500).
+ A 422 adds `detail`, a one-item body-location validation error; `error` retains the message.
- `GET /v1/models` returns `{"models": [{"name", "description", "release_date", "mode", "early_exit",
"temperature_per_prim"}]}` (`release_date` is the engine's release date, not the model's).
</code_context>
<issue_to_address>
**Error shape docs conflict**
When a client is implemented from the FORMAT2 or SERVING error descriptions, the current serving guides still say errors have only `{"error": ...}` and that 422, 503, and 500 share that body, so clients following those descriptions can reject the new `code` field and 422 `detail` field.
Update the current FORMAT2 and SERVING error descriptions to include `code` and explain that only 422 includes `detail`.
</issue_to_address>| max_error = q.get("max_error") | ||
| if max_error is not None: | ||
| try: | ||
| max_error = float(max_error) |
There was a problem hiding this comment.
🟡 Medium · Invalid targets reach inference
When an act request supplies a boolean, non-finite value, or numeric max_error outside (0, 1), float(max_error) accepts these values, so act_spec passes them to inference; certified finds no matching threshold, causing a defer action to be selected or a RequestError/422 only after inference.
Reject boolean, non-finite, and out-of-range max_error values in act_spec before inference.
Prompt for AI agents
In `basal/server.py` at line 287:
**Invalid targets reach inference**
When an `act` request supplies a boolean, non-finite value, or numeric `max_error` outside `(0, 1)`, `float(max_error)` accepts these values, so `act_spec` passes them to inference; `certified` finds no matching threshold, causing a defer action to be selected or a `RequestError`/422 only after inference.
Reject boolean, non-finite, and out-of-range `max_error` values in `act_spec` before inference.| Every error body also has a `code`: `INVALID_REQUEST` (422), `OUT_OF_MEMORY` (503) or `INTERNAL_ERROR` (500). | ||
| A 422 adds `detail`, a one-item body-location validation error; `error` retains the message. |
There was a problem hiding this comment.
⚪ Low · Error shape docs conflict
When a client is implemented from the FORMAT2 or SERVING error descriptions, the current serving guides still say errors have only {"error": ...} and that 422, 503, and 500 share that body, so clients following those descriptions can reject the new code field and 422 detail field.
Update the current FORMAT2 and SERVING error descriptions to include code and explain that only 422 includes detail.
Prompt for AI agents
In `docs/API.md` at lines 84-85:
**Error shape docs conflict**
When a client is implemented from the FORMAT2 or SERVING error descriptions, the current serving guides still say errors have only `{"error": ...}` and that 422, 503, and 500 share that body, so clients following those descriptions can reject the new `code` field and 422 `detail` field.
Update the current FORMAT2 and SERVING error descriptions to include `code` and explain that only 422 includes `detail`.|
Thank you, Paweł! Planned for basal 2.5: we are merging this into the 2.5 release branch now, after our checks (full CPU suite, success responses byte-identical, |
… version CONTRIBUTING.md credits the 2.5 pull requests as the release notes' community line lists them (Paweł Kiszczak #52, #62, #64, #66, #68, #71, #73, #75; Marcin Brzeżański #56; Grzegorz Brzezinka #76, #78) and adds Dariusz Koryto (#53, #58, #59, #60 and #55); the README's list names him too. docs/changes/README.md and the changelog.py help use 2.5.1, the next release, as the example.
Closes #61
What
act.max_errorand threshold-objective set bounds that cannot convert toint()during request parsing, before model inference.codeto failed responses:INVALID_REQUESTfor 422,OUT_OF_MEMORYfor 503, andINTERNAL_ERRORfor 500. Keep the existingerrorfield unchanged and add one-itemdetailonly for 422 responses.Why
Clients can correct invalid requests without invoking the model and can identify failure categories programmatically.
Compatibility
Successful response bodies are unchanged. Error bodies retain
errorand addcode; 422 bodies additionally adddetail. Malformed JSON follows the existing RequestError path and receivesINVALID_REQUEST.Tests run
uv run --no-sync pytest -q tests/test_http_status.py tests/test_server.py— 80 passed.uv run --no-sync pytest -q -x— 764 passed, 53 skipped, 1 warning.uvx [email protected] check --select E9,F63,F7,F82 --target-version py310 .— failed on existingF821 Undefined name kposin untouchedbasal/gemma4.py:471.Summary by Sourcery
Validate malformed serving requests before inference and provide machine-readable error classifications.
Bug Fixes:
max_errorvalues and threshold set bounds during request validation so malformed requests return 422 before model inference.Enhancements:
Documentation:
Tests: