nemo_gym.client.http_client
nemo_gym.client.http_client
The shared aiohttp clients every Gym process uses to call HTTP endpoints, and requests with retries.
Module Contents
Classes
Functions
Data
DISCONNECTED_CLIENT_OS_HELP_TEXT
DISCONNECTED_CLIENT_OS_PRINT_INTERVAL
_GLOBAL_AIOHTTP_CLIENT_QUEUE_TELEMETRY
_GLOBAL_AIOHTTP_CLIENT_REQUEST_DEBUG
_GLOBAL_AIOHTTP_CONTROL_CLIENT
_NUM_SERVER_DISCONNECTED_ERROR
_UPSTREAM_ERROR_LOG_BODY_CHARS
API
Bases: BaseModel
Bases: NamedTuple
Close clients detached from a loop other than the running one, on their owner loop while it still runs.
Clear the process-wide data and control clients and return the ones still open.
Render a bounded upstream error body without hiding its useful traceback.
Strip the query string from a URL before it becomes a span attribute.
Query strings in Gym carry API keys and, on rollout-prefixed routes, task content. The path is the useful part for a trace; the query is a leak waiting to happen.
Whether the one observed HTTP attempt finished sending its body, including EOF.
Pending/cancelled state or an unobserved aiohttp resend is unknown; it cannot qualify a response timeout.
_request_with_retries wrapped in a CLIENT span, with traceparent injected.
Uses nemo_gym.telemetry.spans.client_span rather than nemo-lens’s managed_span
because the latter cannot set SpanKind — see that module for why an INTERNAL span
is wrong on a cross-service hop.
Injection happens inside the span so the header carries this span as the parent — the receiving server’s FastAPI instrumentation extracts it and its SERVER span becomes our child. That edge is what makes one rollout a single trace across three processes.
Retries reuse the same span rather than starting one per attempt: the caller asked for one logical request, and the retry count is recorded as an attribute instead.
Close the process-wide data and control clients on their owning event loop.
Format an exception, appending its bounded HTTP response body when available.
str(ClientResponseError) names only the status, reason phrase, and URL. The upstream server’s own reason,
such as an agent’s AgentSeedRejectedError detail, is in the body that raise_for_status keeps on
response_content.
The reserved pool for control calls, created on first use next to the data client.
Raise a valid remote NgFailureEvent with its original IDs and explicit causes.
error.http_error retains this hop’s status, body and request information. Without a final remote
failure, raise the original ClientResponseError. Call raise_for_status where callers require
that HTTP exception type even for typed replies, such as a model client’s retry/routing logic.
Make an outbound HTTP call through Gym’s shared aiohttp client.
The private transport-failure observer runs before retry decisions and must not raise. It is not forwarded to aiohttp; cancellation bypasses it.
This is the only place Gym talks to another server, so it is also the only place
trace context has to be injected: every agent -> model and agent -> resources hop goes
through here. CLAUDE.md bans httpx precisely to keep it that way.
_server_name is the bounded logical destination label for pool metrics: a configured
ServerClient server name, remote_agent_service for the remote agent’s external
service, or None for the fallback label external. It is retained across retries
and redirects and is not forwarded to aiohttp.
_max_num_tries caps this call’s total attempts on every exception path. It replaces the
default generic-error limit (MAX_NUM_TRIES attempts for external calls, unbounded for
internal ones). A _max_connection_retries limit still applies as well.
_resend_after_disconnect=False raises a dropped connection instead of sending the request again, because the
server may already have received it. A connection that was never made is still retried.
_retry_other_errors=False resends only a dropped or refused connection (ServerDisconnectedError,
ClientOSError) within the caller’s bound; any other error, a timeout or a body cut off mid-read included,
is raised at once.
_read_body=True reads a success reply’s body before returning, but only within a _max_num_tries or
_max_connection_retries bound and only when resending is allowed, so a reply cut off mid-body is sent again
within that bound. Otherwise, and for an error reply, the response is returned unread: the caller’s own read gets
the complete body or raises the original error, so the caller’s failure handling (for collection, its rollout
attempt budget) decides whether the request runs again.