Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion maestro_worker_python/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,23 @@ async def validation_error_handler(request: Request, exc: ValidationError):
return JSONResponse(status_code=400, content=jsonable_encoder({"error": exc.reason}))


def _describe_validation_errors(errors: list) -> str:
"""One line a caller can read, e.g. ``stems: Input should be a valid integer; outputs.midi: Field required``."""
described = []
for error in errors:
# A model-level validator reports an empty location.
field = ".".join(str(part) for part in error["loc"]) or "request"
described.append(f"{field}: {error['msg']}")
return "; ".join(described)


@app.exception_handler(pydantic.ValidationError)
async def pydantic_validation_exception_handler(request: Request, exc: pydantic.ValidationError):
errors = exc.errors()
# `error` carries the reason a caller reads, as it does for ValidationError.
return JSONResponse(
status_code=400,
content=jsonable_encoder({"detail": exc.errors()}),
content=jsonable_encoder({"error": _describe_validation_errors(errors), "detail": errors}),
)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "maestro-worker-python"
version = "5.2.1"
version = "5.2.2"
description = "Utility to run workers on Moises/Maestro"
readme = "README.md"
requires-python = ">=3.10,<3.14"
Expand Down
14 changes: 14 additions & 0 deletions tests/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,20 @@ def test_a_recoverable_failure_keeps_the_process_serving(tmp_path, monkeypatch,
assert terminations == []


def test_an_invalid_request_is_refused_with_a_reason_naming_the_field(tmp_path, monkeypatch):
_, client, _ = _serve_failing_worker(
tmp_path,
monkeypatch,
"import pydantic; type('Request', (pydantic.BaseModel,), {'__annotations__': {'stems': int}})(stems='x')",
)

refused = client.post("/inference", json={})

assert refused.status_code == 400
assert refused.json()["error"].startswith("stems: ")
assert refused.json()["detail"][0]["loc"] == ["stems"]


def _start_with_json_logging(tmp_path, then: str = "") -> subprocess.CompletedProcess:
"""Import serve.py in a fresh interpreter: json_logging configures process-wide state once."""
worker_path = tmp_path / "worker.py"
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading