nemo_gym.anthropic_utils
nemo_gym.anthropic_utils
NeMo Gym Pydantic types for the Anthropic Messages API boundary.
This mirrors the wrapping strategy in nemo_gym/openai_utils.py, but deliberately stays
small. Anthropic is only a wire format at the proxy boundary — the internal canonical
representation is the Responses API (the NeMoGym* types in openai_utils). So unlike
openai_utils, this module does not replicate the *ForTraining hierarchies or an async
client (the anthropic SDK is never used as a client — it uses httpx, whose O(n^2)
connection pooling hangs at high concurrency). Types only.
Two boundary types, one per direction:
- :class:
NeMoGymAnthropicMessageCreateParamsNonStreaming— the request validator. The SDK’sMessageCreateParamsis aTypedDictwith no runtime validation, so we copy it as aBaseModelfor strict server-side validation at the ingress proxy endpoint, exactly asNeMoGymResponseCreateParamsNonStreamingdoes for the Responses API. - :class:
NeMoGymAnthropicMessage— the response validator. A thin subclass of the SDK’sMessage(already aBaseModel), used to validate what we emit, mirroringNeMoGymResponse(Response).
Module Contents
Classes
API
Bases: AnthropicMessage
Thin subclass of the SDK’s Message response model, used to validate what we emit.
Message.content is already typed as List[...] in the pinned anthropic version,
so no iterable override is needed here; the subclass exists for symmetry with
NeMoGymResponse and to give the egress/ingress response path a single NeMoGym*
validation point.
Bases: BaseModel
Copy of anthropic.types.message_create_params.MessageCreateParamsBase as a BaseModel.
The SDK ships this as a TypedDict (no runtime validation). We need server-side
validation at the ingress proxy, so we re-declare it here, mirroring
NeMoGymResponseCreateParamsNonStreaming.
The Iterable fields (messages, tools, and the list arm of system) are
overridden to List so Pydantic eagerly validates them into real, re-iterable,
indexable, JSON-serializable lists rather than single-use lazy ValidatorIterators.
Note on extra="forbid": this matches the strict Responses-API policy and rejects
unknown fields. Anthropic occasionally introduces beta body fields ahead of an SDK bump;
if the ingress client (e.g. the Claude Code CLI) sends one, relax this to ignore.