nemo_gym.models.anthropic_utils
nemo_gym.models.anthropic_utils
NeMo Gym Pydantic types for the Anthropic Messages API boundary.
This mirrors the wrapping strategy in nemo_gym/core/responses_api_types.py, but deliberately stays small.
Anthropic is only a wire format at the proxy boundary.
Internally, Gym uses the Responses API: the NeMoGym* types in that module.
Unlike those types, this module does not replicate the *ForTraining hierarchies.
Unlike nemo_gym/client/openai_client.py, it provides no async client.
The anthropic SDK is never used as a client, because it uses httpx.
httpx’s O(n^2) connection pooling hangs at high concurrency.
Types only.
Two boundary types, one per direction:
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.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.