judgevet.adapters.outbound.response_translation

Status: draft.

judgevet.adapters.outbound.response_translation

Translate a raw Jev response into a parsed answer or a JevError.

The functions take a status code, the raw body bytes and a status line, so they need no HTTP client. An HTTP adapter passes response.content and the string form of its status error.

The API error detail field is polymorphic:

  • Array for validation errors (422): [{"type", "loc", "msg", "input"}]
  • Object for auth errors (401/403): {"error_type", "message"}
  • Object for an oversized request (400 observed): {"error_type"} equal to "max_tokens_exceeded", returned as JevMaxTokensExceededError
  • Absent, malformed or unrecognised: falls back to the status line alone.

The input key in validation errors contains the caller's request payload and is deliberately excluded from error messages to avoid leaking user data.

Examples:

from judgevet import JevError
from judgevet.adapters.outbound.response_translation import (
    parse_success,
    translate_status,
)

error = translate_status(302, b"", "Found")
assert isinstance(error, JevError)

body = (
    b'{"model": "jev-1.13.0", "answers": {}, '
    b'"usage": {"input_tokens": 10, "output_tokens": 0}}'
)
parsed = parse_success(200, body, {})
assert parsed.model == "jev-1.13.0"
See Also

parse_success(status_code, content, questions)

Parse a successful JSON body and bind it to the request's questions.

Parameters:

Name Type Description Default
status_code int

The HTTP status code of the response.

required
content bytes

The raw response body.

required
questions Mapping[str, Any]

The request's questions, which bound each Choice option.

required

Returns:

Type Description
SystemOneResponse

A parsed SystemOneResponse.

Raises:

Type Description
JevResponseError

If the body is not valid JSON, does not parse, or names a Choice option outside its question's criteria.

translate_status(status_code, content, message)

Translate an error status and its body to a JevError subclass.

A 4xx body whose detail.error_type is max_tokens_exceeded becomes JevMaxTokensExceededError. One live call returned that marker with 400. Source: https://github.com/Alberto-Codes/judgevet/issues/39#issuecomment-5825759575.

Parameters:

Name Type Description Default
status_code int

The HTTP status code of the response.

required
content bytes

The raw error response body.

required
message str

The status line the error message starts with.

required

Returns:

Type Description
JevError | None

A JevError instance for every non-success status, with specialized

JevError | None

subclasses for the mapped errors. None for successful 200-299 statuses.

HTTP status classes follow RFC 9110. Source: https://www.rfc-editor.org/rfc/rfc9110.html#section-15.