Gym icon indicating copy to clipboard operation
Gym copied to clipboard

feat: Add anthropic messages api support as a responses_api_model.

Open ffrujeri opened this issue 5 months ago • 1 comments

What does this PR do ?

Design Note

Adds a native anthropic_model Responses API model server that translates NeMo Gym requests to Anthropic /v1/messages and maps Anthropic responses back to NeMo Gym Responses API objects.

Issues

Closes #1146

Usage

  • Configure anthropic_model with an Anthropic base URL, API key, model name, and max token limit:
ng_run "+config_paths=[responses_api_models/anthropic_model/configs/anthropic_model.yaml,<resources_server_config>.yaml]" \
  +policy_base_url=https://api.anthropic.com/v1 \
  +policy_api_key="$ANTHROPIC_API_KEY" \
  +policy_model_name=claude-opus-4-8

Once the servers are up, direct model-server smoke test:

curl -s http://127.0.0.1:<policy_model_port>/v1/responses \
  -H 'Content-Type: application/json' \
  -d '{
    "input": "Say hello in one short sentence.",
    "max_output_tokens": 64
  }' | python -m json.tool

Minimal end-to-end smoke test with the example single-tool-call environment:

ng_run "+config_paths=[resources_servers/example_single_tool_call/configs/example_single_tool_call.yaml,responses_api_models/anthropic_model/configs/anthropic_model.yaml]" \
  +policy_base_url="$ANTHROPIC_BASE_URL" \
  +policy_api_key="$ANTHROPIC_API_KEY" \
  +policy_model_name="$ANTHROPIC_MODEL_NAME"

Collect one rollout through the simple agent:

mkdir -p results

ng_collect_rollouts \
  +agent_name=example_single_tool_call_simple_agent \
  +input_jsonl_fpath=resources_servers/example_single_tool_call/data/example.jsonl \
  +output_jsonl_fpath=results/claude_example_single_tool_call_rollouts.jsonl \
  +limit=1 \
  +num_repeats=1

Additional Information

  • Uses raw aiohttp transport through nemo_gym.server_utils.request() instead of the Anthropic Python SDK.
  • Supports Responses API text messages, base64 image inputs, system/developer prompt extraction, function tool schemas, previous tool calls/results, Anthropic thinking blocks, usage mapping, and request concurrency limiting.
  • Serializes system prompts as Anthropic text blocks, supports typed adaptive thinking config, and keeps extra_body for less common Anthropic pass-through fields.
  • Maps Responses input_image data URLs to Anthropic image blocks for image/jpeg, image/png, image/gif, and image/webp; remote image URLs and invalid base64 are rejected with clear 400 errors.
  • Maps Anthropic stop reasons to Responses-compatible incomplete_details: max_tokens and model_context_window_exceeded become max_output_tokens; refusal becomes content_filter.
  • Fails fast for Claude Opus 4.7/4.8 sampling restrictions when temperature, top_p, or top_k are explicitly configured.
  • Adds mocked tests for Anthropic request shape, response conversion, tool use round trips, image conversion/validation, thinking/reasoning mapping, refusal/stop-reason mapping, sampling validation, and semaphore behavior.
  • Verified with:
    • uv run ruff format responses_api_models/anthropic_model
    • uv run ruff check responses_api_models/anthropic_model
    • uv run pytest responses_api_models/anthropic_model/tests/test_app.py -q

ffrujeri avatar Jun 08 '26 22:06 ffrujeri

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

copy-pr-bot[bot] avatar Jun 08 '26 22:06 copy-pr-bot[bot]

did you test with claude_code_agent and would you mind updating that agent instructions to use this if so?

So, this PR only implements the ResponsesAPI <--> Anthropic Messages. I think what you mean by this question is the other direction of translation that would allow Anthropic Messages <--> ResponsesAPI could be handled in a different Issue and PR.

ffrujeri avatar Jun 15 '26 23:06 ffrujeri