Skip to content

refactor(models): split model call capture out of the model server base and move the OpenAI client into client/ - #5225

Merged
ananthsub merged 1 commit into
mainfrom
ananthsub/layout-model-base
Nov 2, 2026
Merged

ananthsub merged 1 commit into
mainfrom
ananthsub/layout-model-base

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Moves base_responses_api_model.py into nemo_gym/models/ and splits its model call capture into its own module. The file held both the model server base classes and the middleware that records every model call, along with that middleware's store. After this PR, each lives in its own module, and models/ holds the whole model server.

It also moves the OpenAI-compatible client, models/openai_utils.py, to client/openai_client.py. The client calls model endpoints over the shared HTTP client in client/http_client.py, so it belongs with the other clients rather than with the model server. It used to import ClientResponseError from the server runtime, which made the client layer depend on a higher layer. It now imports it from aiohttp directly.

Where this PR sits

The stack lands bottom-up; each PR's base is the branch of the PR before it, so each diff shows only that PR's change.

flowchart LR
  compat["#5167<br/>Old import paths<br/>keep working"]
  linter["#5168<br/>import-linter<br/>layer contract"]
  prep["#5169<br/>Remove path and<br/>name couplings"]
  config["#5189<br/>Move config/"]
  utils["#5195<br/>Move utils/"]
  core["#5203<br/>Move core/"]
  client["#5206<br/>Move client/,<br/>start telemetry/"]
  trajectory["#5209<br/>Move trajectory/"]
  verification["#5210<br/>Move verification/"]
  metrics["#5211<br/>Move metrics/"]
  artifacts["#5212<br/>Move artifacts/"]
  srmods["#5214<br/>Move server_runtime/<br/>modules"]
  su["#5215<br/>Split server_utils"]
  model["#5224<br/>Move models/<br/>modules"]
  modelbase["#5225<br/>Split model<br/>call capture"]
  agents["#5228<br/>Move agents/"]
  data["#5229<br/>Move data/"]
  harbor["#5230<br/>Split the<br/>Harbor task CLI"]
  rollouts["#5231<br/>Move rollouts/"]
  rsplit["#5232<br/>Split the<br/>rollout collector"]
  testing["#5233<br/>Dissolve testing/"]
  orch["#5234<br/>Move orchestration/<br/>modules"]
  sup["#5235<br/>Split the<br/>server supervisor"]
  eval["#5244<br/>Move eval/"]
  env["#5298<br/>Move environment/<br/>registries"]
  cli["#5300<br/>Retire the<br/>cli_setup_command shim"]
  docs["#5385<br/>Describe the<br/>package layout"]
  owners["next<br/>CODEOWNERS:<br/>per-package groups"]
  remove["0.10.0<br/>Remove the<br/>old paths"]
  compat --> linter
  linter --> prep
  prep --> config
  config --> utils
  utils --> core
  core --> client
  client --> trajectory
  trajectory --> verification
  verification --> metrics
  metrics --> artifacts
  artifacts --> srmods
  srmods --> su
  su --> model
  model --> modelbase
  modelbase --> agents
  agents --> data
  data --> harbor
  harbor --> rollouts
  rollouts --> rsplit
  rsplit --> testing
  testing --> orch
  orch --> sup
  sup --> eval
  eval --> env
  env --> cli
  cli --> docs
  docs --> owners
  owners --> remove
  classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937
  classDef later fill:#f3f4f6,stroke:#9ca3af,color:#4b5563,stroke-dasharray: 4 3
  class modelbase this
  class owners,remove later
Loading

What moves

flowchart LR
  subgraph B["base_responses_api_model.py today"]
    S["SimpleResponsesAPIModel and config,<br/>request validation and dispatch"]
    C["Capture middleware, CaptureStore,<br/>SSE reconstruction"]
  end
  K["chat_streaming.py:<br/>SSE_KEEPALIVE"]
  O["models/openai_utils.py:<br/>NeMoGymAsyncOpenAI"]
  S --> S2["models/base_responses_api_model.py"]
  C --> C2["models/model_call_capture.py"]
  K --> K2["core/responses_api_types.py"]
  O --> O2["client/openai_client.py"]
  classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937
  class S2,C2,K2,O2 this
Loading
  • Git pairs the old base_responses_api_model.py with models/model_call_capture.py, because most of the file's lines were capture code. models/base_responses_api_model.py shows as a new file. git blame -C -C follows its lines back to the old file.
  • SSE_KEEPALIVE, the liveness-only SSE frame, moves to core/ as the proposal places it: the checkpoint gate and the dispatch both compare against it.
  • client/openai_client.py is a git rename of models/openai_utils.py, and its test moves to tests/unit_tests/client/test_openai_client.py. NeMoGymAsyncOpenAI, PermanentEndpointError, RATE_LIMIT_ERROR_CODES, RETRY_ERROR_CODES and MAX_NUM_TRIES are exported from nemo_gym.client and no longer from nemo_gym.models.

What stays importable from the old paths

  • nemo_gym.base_responses_api_model is the same module object as nemo_gym.models.base_responses_api_model.
  • Every capture name resolves at the old path with one warning, and SSE_KEEPALIVE at nemo_gym.chat_streaming and nemo_gym.models.chat_streaming.
  • The module already served names that moved earlier, such as ModelCallRecord. Their warning names the path the caller imported, nemo_gym.base_responses_api_model, as it did before this PR. moved_attr_getter in nemo_gym/utils/compat.py now names the old path of a module that itself moved, which also fixes the same message in metrics/reward_profile.py.
  • nemo_gym.openai_utils now resolves to nemo_gym.client.openai_client, with one warning.
  • nemo_gym.models.openai_utils gets no old-path entry. It was added by refactor(models): move the model clients, converters and token capture into nemo_gym/models #5224 and has not been in a release.

How to review

One commit. Its changes:

  1. Renames:
    • base_responses_api_model.py becomes models/model_call_capture.py in git's pairing (see above).
    • models/openai_utils.py becomes client/openai_client.py.
  2. Everything else:
    • The model server base classes go into models/base_responses_api_model.py. The capture code moves unchanged into models/model_call_capture.py, which gets its own module logger, and the base module imports back the three names it uses.
    • SSE_KEEPALIVE moves to core/responses_api_types.py, and chat_streaming.py gains a __getattr__ that serves it.
    • The moves are recorded in nemo_gym/_moved_paths.py, including the names base_responses_api_model.py imported only for code that moved out. Each name routes to the module that defines it, so migrate_imports rewrites to the public from nemo_gym.models import ....
    • In-repo references are rewritten by scripts/migrate_imports.py. By hand:
      • the lazy exports in nemo_gym/__init__.py;
      • two capture tests that patched through the old module;
      • the contract's model layer (now the nemo_gym.models package alone);
      • the docs redirects, including one from the models/openai_utils reference page;
      • the server imports of the client names, now from nemo_gym.client;
      • the links and comments that named the old files.
  3. Tests:
    • The model server tests move into tests/unit_tests/models/.
    • The legacy-import test keeps the old path and checks the warning's text, including the renamed token_id_capture_config.

History of split files

The repository squash-merges, so this PR lands as one commit. Whole-module moves still show as renames. A split file does not: its old path stays with less in it, so the new file starts its own history. git blame -C -C <new file> follows the moved lines back to the old file, and git log --follow does not.

Public API

Every layout package that exists after this PR has a lazy public API in its __init__.py: the names code outside nemo_gym/ imports from it (servers and their tests, benchmarks, scripts, and code samples in docs and skills), plus every name it already exported, each served from the submodule that defines it on first use. Code outside nemo_gym/ and its tests (servers and their tests, benchmarks, scripts, and docs) imports those names from nemo_gym.<package>, so later moves inside a package leave it alone. Code inside nemo_gym/, and the library's tests under tests/, import from the module that defines each name, so the import-linter contract sees every dependency and a test reads what it patches. A patch target names the module the code under test looks the name up in. AGENTS.md states the rule.

nemo_gym.models also exports FastAPI's Body. Servers in 0.7.0 imported it from the server base modules, so code that migrate_imports rewrites from those paths needs a public name to point at.

Validation

  • pytest -n 16 tests/unit_tests, also with -W error for the old base_responses_api_model and chat_streaming paths and names.
  • The legacy-import test fails without the warning-text fix in utils/compat.py and passes with it.
  • lint-imports.
  • pre-commit run on the changed files.
  • Server and agent tests run in CI.

Rollout evidence: N/A. The code moves unchanged.

Compatibility

  • Old paths work until 0.10.0, with one warning each, and give the same objects.
  • Logger names change with the modules: the model server base logs under nemo_gym.models.base_responses_api_model and model call capture under nemo_gym.models.model_call_capture. A logging config set on nemo_gym still covers both.
  • Patches that stop taking effect:
    • A patch through base_responses_api_model of a capture name no longer reaches the capture code; patch nemo_gym.models.model_call_capture.
    • token_capture_config, _SSE_KEEPALIVE and uuid4 are bound in both modules. A patch reaches only the module it names, so patch the one whose code you are testing.
    • A patch of request or sleep through nemo_gym.models.openai_utils must name nemo_gym.client.openai_client.
  • Open branches: run python scripts/migrate_imports.py <paths>.

Issue

Part of #5166.

@ananthsub
ananthsub added this pull request to stack #5170 October 28, 2026 21:24
@copy-pr-bot

copy-pr-bot Bot commented Oct 28, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@ananthsub ananthsub changed the title ananthsub/layout model base refactor(model): split model call capture out of the model server base Oct 28, 2026
@ananthsub ananthsub added feature New capabilities, enhancements, or enablement work area:core Shared APIs, servers, telemetry, health, and registries needs-review PR is ready for code review and waiting on a reviewer labels Oct 28, 2026
@ananthsub
ananthsub requested a review from yaoyu-33 October 28, 2026 21:27
@ananthsub
ananthsub marked this pull request as ready for review October 28, 2026 21:27
@ananthsub
ananthsub requested a review from a team as a code owner October 28, 2026 21:27
@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 28, 2026 21:27
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

@ananthsub
ananthsub force-pushed the ananthsub/layout-model-base branch from 09667f8 to e634541 Compare October 28, 2026 21:56
@ananthsub ananthsub changed the title refactor(model): split model call capture out of the model server base refactor(models): split model call capture out of the model server base Oct 28, 2026
@ananthsub
ananthsub requested review from a team as code owners October 28, 2026 22:26
@ananthsub
ananthsub force-pushed the ananthsub/layout-model-base branch from e634541 to 187f38c Compare October 28, 2026 22:26

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this split. We reviewed it at 6dff4dd122ae and found nothing blocking; every inline comment is a NOTE.

What we checked

  • Every top-level definition from the old nemo_gym/base_responses_api_model.py landed in exactly one of the two new modules, AST-identical apart from imports. The only new code is the wrapper __getattr__.
  • SimpleResponsesAPIModel installs the same middleware, routes, lifespan and exception handlers, in the same order, before and after. We compared four capture and checkpoint configurations.
  • Module-level state exists once. SSE_KEEPALIVE is a single object across core, the capture module, _checkpoint and both old paths.
  • There is no new import cycle, and lint-imports is clean. Reading CaptureStore no longer loads the server base, which roughly halves the import.
  • Every name the old module served resolves at the old path to the same object, and the in-repo patch targets still reach the code they patch.
  • CI is green apart from the non-blocking kilocode conformance probe.

PR description

  • [PROCESS] The history note is reversed. git pairs the old file with models/model_call_capture.py (rename, 51% similarity) and treats models/base_responses_api_model.py as a new file. GitHub also shows the base as a new 998-line file, so step 1, "Rename only", is not in the diff. After the squash, git log --follow on the base stops at this commit, while model_call_capture.py carries the old file's history; git blame -C -C follows the base back. The "What moves" diagram and step 1 also say model/ for models/.
  • [PROCESS] #5166 asks each split PR to list the patch targets that stop taking effect. The Compatibility section covers capture names in one line. Could it list the names, and add two cases it misses? No in-repo patch target is affected.
    • Names bound in both modules: token_capture_config, _SSE_KEEPALIVE and uuid4. A patch through the base module reaches only the base's own use and gives no warning. For example, install_model_call_capture reads token_capture_config from the capture module, while setup_policy_checkpoint reads it from the base.
    • The base module's logger: its name also changes, to nemo_gym.models.base_responses_api_model.

Outside the diff

  • config_types.py:1028 still says the capture middleware in base_responses_api_model.py strips the rollout prefix. That middleware is now in models/model_call_capture.py.

Across the stack

  • #5228 adds the same "Body": "base_responses_api_agent" entry to nemo_gym.agents, so whatever is decided for Body here applies there too.
  • The legacy-warning path issue at base_responses_api_model.py:958 also affects metrics/reward_profile.py (from #5211) and every module that moves later with a moved_attr_getter(__name__, ...) shim (#5229, #5231, #5232). The nemo_gym/utils/compat.py change suggested inline fixes all of them.

Comment thread nemo_gym/models/__init__.py Resolved
Comment thread nemo_gym/_moved_paths.py Resolved
yaoyu-33
yaoyu-33 previously approved these changes Nov 2, 2026

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 6dff4dd122ae. The team review found no blockers or risks; the remaining inline notes are optional and can be handled here or in a follow-up.

…se and move the OpenAI client into client/

base_responses_api_model.py held both the model server base classes and the middleware that
records every model call, along with that middleware's store.
The base classes now live in models/base_responses_api_model.py, and the capture middleware and its
store move unchanged to models/model_call_capture.py, which gets its own logger.
Git pairs the old file with model_call_capture.py, because most of its lines were capture code.

The liveness-only SSE frame SSE_KEEPALIVE moves from models/chat_streaming.py to
core/responses_api_types.py, because the checkpoint gate and the dispatch both compare against it.

models/openai_utils.py moves to client/openai_client.py, because it calls model endpoints over the
shared HTTP client and is not part of the model server.
It now imports ClientResponseError from aiohttp instead of from the server runtime, which pointed up
the layer order.
nemo_gym.openai_utils resolves to the new module.
nemo_gym.models.openai_utils gets no old-path entry, because no release contained it.
The client names are exported from nemo_gym.client, and servers import them from there.

The module and name moves are recorded in MOVED_MODULES and MOVED_NAMES.
Each moved name routes to the module that defines it, so migrate_imports rewrites old imports to the
public nemo_gym.models API.
The deprecation warning for a name served by a module that itself moved now names the path the
caller imported.
In-repo references are rewritten with scripts/migrate_imports.py, and the lazy exports, two capture
tests, the docs links and the library reference redirects in fern/docs.yml point at the new modules.

The public API of each layout package that exists here is regenerated from what code outside the
library imports.
Code outside nemo_gym and its tests imports from nemo_gym.<package>, and the library's tests import
from the module that defines each name.

Signed-off-by: Ananth Subramaniam <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 846ce0a5 Deployed Nov 2, 2026 by copy-pr-bot[bot] via release / finalize / notify #9329
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Shared APIs, servers, telemetry, health, and registries feature New capabilities, enhancements, or enablement work ready-to-merge PR is approved, current, and waiting for final CI or merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants