Skip to content

Add a default /v1/messages (Anthropic Messages) route to the base Gym model server #1620

Closed

Description

@ffrujeri

Use cases, pain points, and background

Gym standardizes on the OpenAI Responses API: every model server implements /v1/responses,
and Gym-native agents speak it. But real external/blackbox harnesses speak other protocols —
the Claude Code CLI only talks the Anthropic Messages API (/v1/messages), so today it can
only reach Anthropic's own endpoint. Users want to run such harnesses against any Gym
backend (vLLM, OpenAI, hosted inference providers) for evaluation.

This is the inverse direction of the claude_model / anthropic_model provider
(#1146, #1546), which lets Gym use Claude as a backend. Here we want Gym model
servers to serve Anthropic Messages so an Anthropic-speaking client can use any backend.

It is explicitly called for in the model-proxy target state (#1285) and assumed
by the CustomAgent EPIC (#1042):

Blackbox agent harnesses will call any common schema that exists (e.g. including Anthropic Messages).
Add a /v1/messages route on every Gym model proxy that by default maps Messages ↔ Responses.

The EPIC's sidecar model proxy is "path-preserving forward to the Gym model server" — it needs
a /v1/messages route on the server to forward to.

Description

Add a POST /v1/messages route to the base SimpleResponsesAPIModel, with a default
implementation that maps Anthropic Messages ↔ Responses around the server's own responses().
Every model server (vLLM, OpenAI, inference_provider, …) then speaks Anthropic Messages with no
per-server work, and claude_code_agent can target any of them via its model_server ref.

Key translation concerns (handled by a shared converter):

  • Message format — Anthropic role-scoped content blocks (text, tool_use, tool_result,
    thinking) vs Responses item-oriented input/output.
  • System prompt — Anthropic top-level system (string or text-block array) ↔ Responses
    instructions + system/developer items.
  • Tool use — tool_use/tool_result content blocks ↔ Responses function_call /
    function_call_output; tool schema ↔ input_schema.
  • Thinking/reasoning — Anthropic thinking blocks ↔ Responses reasoning items.
  • Streaming — the Claude CLI requires SSE; the backend is non-streaming through Responses,
    so the server synthesizes the Anthropic SSE event stream from a complete response.

Design

Base class change

class SimpleResponsesAPIModel(BaseResponsesAPIModel, SimpleServer):
    def setup_webserver(self) -> FastAPI:
        app = FastAPI()
        self.setup_session_middleware(app)
        app.post("/v1/chat/completions")(self.chat_completions)
        app.post("/v1/responses")(self.responses)
        app.post("/v1/messages")(self.messages)   # NEW: default Messages <-> Responses
        return app

    async def messages(self, request, body):
        # anthropic_request_to_responses -> self.responses(...) -> responses_to_anthropic_response
        # -> synthesized Anthropic SSE if stream:true

messages() is concrete and overridable; it delegates to the server's own responses()
(dispatching on its signature, since some take (body) and some (request, body)).

Shared bidirectional converter

nemo_gym/anthropic_converter.py (transport-free, no anthropic SDK — raw aiohttp via
nemo_gym.server_utils.request() per CLAUDE.md), shared with the egress anthropic_model
provider so the mapping rules exist once:

  • egress: responses_to_anthropic, anthropic_to_responses
  • ingress: anthropic_request_to_responses, responses_to_anthropic_response,
    anthropic_response_to_sse

Anthropic-API-as-backend policy (sampling-restriction allowlist, thinking-mode guards,
base-URL normalization, lossy cache-token accounting) stays in the anthropic_model server,
not the shared converter.

Files

  • nemo_gym/base_responses_api_model.py — add /v1/messages + default messages()
  • nemo_gym/anthropic_converter.py — shared bidirectional converter
  • resources_servers/reasoning_gym/configs/reasoning_gym_claude_code_agent_model_server.yaml —
    showcase wiring claude_code_agent.model_server → policy_model
  • tests for the converter (both directions + round-trip) and the base-class route

Out of scope

  • RL token-ids / logprobs through the Messages path (eval-first; token-ids live only at the
    Responses hop and cannot ride the Anthropic SSE response — would be side-channeled later).
  • Streaming generation from the backend (SSE is synthesized from a complete response).
  • Multimodal beyond base64 images; computer-use response items.
  • A separate proxy server / messages_api_models category (prototyped and rejected in favor of
    the base-class route).

Acceptance Criteria

  • /v1/messages is served by every SimpleResponsesAPIModel (verified on at least openai_model / vllm_model)
  • Default messages() translates Messages → Responses → Messages around responses(), for both responses() signatures
  • Tool use round-trips (function calls and results); thinking maps to reasoning items; system handled as top-level system
  • SSE event stream synthesized for stream: true and accepted by the Claude Code CLI
  • Shared converter covered by unit tests including round-trip; fail-loud on unsupported content
  • claude_code_agent runs end-to-end against a Gym model server's /v1/messages on an existing environment

References

Activity

  1. ffrujeri commented on Jun 16, 2026

    @ffrujeri
    ContributorAuthor

    Design Note: Anthropic Messages on every Gym model server

    1. Summary

    Gym's native format is the OpenAI Responses API: every Gym model server implements
    /v1/responses, and Gym-native agents speak it. But real external harnesses speak other
    protocols — the Claude Code CLI only talks the Anthropic Messages API (/v1/messages), and
    today it can only reach Anthropic's own endpoint.

    This change makes every Gym model server speak Anthropic Messages by default. A
    /v1/messages route is added to the base SimpleResponsesAPIModel, with a default
    implementation that maps Messages ↔ Responses around the server's own responses(). The
    mapping lives in one shared, transport-free converter (nemo_gym/anthropic_converter.py),
    used in both directions and shared with the egress anthropic_model provider (#1146/#1546).

    Result: a blackbox harness that requires an Anthropic endpoint (Claude Code today; Codex,
    others later) can run against any Gym backend — vLLM, OpenAI, an inference provider —
    with zero per-server work. This is the model-proxy layer the CustomAgent EPIC (#1042) assumes:
    its sidecar's "path-preserving forward to the Gym model server" forwards /v1/messages here.

    2. Goals / Non-goals

    Goals

    • Every Gym model server exposes /v1/messages with a correct default Messages ↔ Responses mapping.
    • One converter, both directions, shared with the egress anthropic_model provider — no duplication.
    • claude_code_agent runs against any Gym model server, end-to-end, for eval.
    • Fail loud on unsupported content rather than silently dropping it.

    Non-goals (this change)

    • RL token-ids through the Messages path (eval-first; see §7).
    • Streaming generation from the backend (we synthesize SSE from a complete response).
    • A separate proxy server, or a new config category (both were prototyped and rejected — §4).
    • Computer-use / non-text multimodal beyond base64 images.

    3. Background

    • Hub format. SimpleResponsesAPIModel exposes /v1/chat/completions and /v1/responses;
      NeMoGymResponse is the canonical (superset of OpenAI Responses) interlingua.
    • claude_code_agent runs claude -p as a subprocess; the CLI calls
      {ANTHROPIC_BASE_URL}/v1/messages (Anthropic Messages, SSE). The harness already resolves
      ANTHROPIC_BASE_URL from a model_server ref — it only needed a server that speaks Messages.
    • Prior art. feat: add inference_provider server for hosted inference providers #1286 extracted the Chat-Completions ↔ Responses converter into
      nemo_gym/responses_converter.py. feat: claude model support #1146/feat: Add anthropic messages api support as a responses_api_model. #1546 added the egress anthropic_model provider
      (Responses → Anthropic) with its own AnthropicConverter. This change makes that converter
      bidirectional and shared, and turns "Messages in" into a base-class capability.

    4. Design

    /v1/messages on the base class. SimpleResponsesAPIModel.setup_webserver() registers a
    third route, and a concrete default messages() does:

    1. anthropic_request_to_responses(body) → Responses create-params.
    2. delegate to the server's own self.responses(...) (so it reuses whatever backend the
      server has — vLLM, OpenAI, …); a small inspect-based dispatch handles the two existing
      responses() signatures ((body) and (request, body)).
    3. responses_to_anthropic_response(result) → Anthropic message.
    4. if the client set stream: true (the Claude CLI always does), re-emit the complete
      response as a synthesized Anthropic SSE event stream.

    Servers may override messages() for native handling.

    Why the base class, not a separate proxy. An earlier iteration built a standalone
    anthropic_messages_proxy server (+ a SimpleMessagesAPIModel base) that forwarded to a
    downstream server over HTTP. Putting the route on the base class instead means every server
    gets Messages for free, with no extra process or network hop, and claude_code_agent points
    directly at any model server. The proxy and the messages-only base class were removed.

    Why synthesize SSE. Backends are non-streaming through Gym's Responses API; the converter
    fabricates the Anthropic event sequence (message_start → per-block deltas → message_stop)
    from the complete response. This satisfies the CLI without requiring streaming in every backend.

    5. The converter (nemo_gym/anthropic_converter.py)

    One AnthropicConverter, transport-free and SDK-free (local Pydantic/dict shapes over
    Gym's aiohttp; the anthropic SDK is avoided for its httpx connection-pool behavior). It is
    bidirectional so the egress provider and this ingress path share one rule set:

    Direction Methods Consumer
    Responses → Anthropic request responses_to_anthropic egress anthropic_model
    Anthropic response → Responses anthropic_to_responses egress anthropic_model
    Anthropic request → Responses anthropic_request_to_responses this /v1/messages
    Responses → Anthropic response responses_to_anthropic_response this /v1/messages
    Anthropic response → SSE anthropic_response_to_sse this /v1/messages

    Supports text, base64 images, function tools, tool_use/tool_result, thinking blocks, and
    stop-reason mapping; raises NotImplementedError on unsupported blocks (fail-loud). Shared
    field-mapping helpers (tool_choice, stop_reason, data-URL/media-type, block↔item) exist once
    and are exercised by round-trip property tests.

    Boundary. Anthropic-API-as-backend policy (sampling-restriction allowlist, thinking-mode
    guards, base-URL normalization, lossy cache-token accounting) stays in the egress
    anthropic_model server — it is not structural mapping and does not apply to an open-model
    backend reached via /v1/messages.

    6. claude_code_agent wiring

    No harness code change: set the agent's model_server ref instead of anthropic_base_url.
    The example config
    resources_servers/reasoning_gym/configs/reasoning_gym_claude_code_agent_model_server.yaml
    points the agent at policy_model; compose it with any model-server config (e.g. vllm_model).

    7. Cross-cutting

    • RL token-ids / faithfulness. Token-ids exist only at the Responses hop; the synthesized
      Anthropic SSE cannot carry them. So this path is eval-first. An RL path would side-channel
      token-ids from the downstream Responses result (out of band), not through the Messages response
      — consistent with the EPIC's conditional RL faithfulness.
    • Cross-schema gaps. Items present in one schema but not another (e.g. Responses
      computer_use vs Messages) are out of scope and fail loud rather than being dropped.

    8. Relationship to other efforts

    9. Testing

    • tests/unit_tests/test_anthropic_converter.py — ingress + round-trip; …_egress.py — egress;
      100% line coverage on the converter.
    • tests/unit_tests/test_responses_api_model_messages.py — base-class /v1/messages for both
      responses() signatures + SSE.
    • Full unit suite (284) green; openai_model's own tests pass unchanged (every server now has a
      working /v1/messages).
    • Live e2e: claude_code_agent → policy_model (vLLM) /v1/messages on a reasoning_gym task —
      finished_naturally=1, 1 turn, ~5.5k tokens, 0 failures. (Reward 0 reflects the small model's
      answer, not the plumbing.)

    10. Open questions (tracked, not solved here)

    • Versioning/regression. OpenAI/Anthropic schemas bump often; the Messages ↔ Responses and
      Chat-Completions ↔ Responses converters need a dedicated regression suite.
    • Audit existing Chat-Completions ↔ Responses tests and align with the new converter coverage.
    • Cross-schema policy. Confirm fail-loud vs drop for items absent in a target schema.
  2. changed the title [-]feat: Add a default `/v1/messages` (Anthropic Messages) route to the base Gym model server[/-] [+]Add a default `/v1/messages` (Anthropic Messages) route to the base Gym model server[/+] on Jun 16, 2026
  3. self-assigned this
    on Jun 23, 2026
  4. ffrujeri commented on Jul 29, 2026

    @ffrujeri
    ContributorAuthor

    This issue can be closed by #1546, if need in the future we can re-open both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions