Skip to content

fix: handle llama.cpp context overflow in model adapter - #3371

Merged
zyzhou5 merged 4 commits into
NVIDIA-NeMo:mainfrom
wefstratis-nv:fix/llamacpp-context-overflow
Oct 8, 2026
Merged

zyzhou5 merged 4 commits into
NVIDIA-NeMo:mainfrom
wefstratis-nv:fix/llamacpp-context-overflow

Conversation

@wefstratis-nv

Copy link
Copy Markdown
Contributor

What does this PR do?

Handle llama.cpp context-overflow errors in the OpenAI-compatible model adapter.

Previously, llama.cpp HTTP 400 errors such as exceed_context_size_error were not recognized and caused evaluation failures. These errors now follow the existing context-length handling: return an empty completion with finish_reason="length".

The change covers both chat-completions and completions paths. Unrelated errors continue to propagate.

Validation:

  • All 177 adapter tests pass, including 26 new regression cases.
  • Pre-commit checks pass for the two changed files. The full --all-files command has not been run.
  • Adapter-only coverage increased from 84.74% to 87.75%, but remains below the configured 95% threshold.
  • No live benchmark validation has been performed.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format/copyright pass).
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

@copy-pr-bot

copy-pr-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

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.

@nemo-automation-bot nemo-automation-bot Bot added the community-request Issue reported or requested by someone from the community label Sep 14, 2026

@ananthsub ananthsub 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 @wefstratis-nv for the contribution! could you address the merge conflicts with the latest main so we can merge?

@yaoyu-33 yaoyu-33 added area:model Model servers, inference providers, and model adapters bug Something isn't working waiting-on-customer Waiting on the original author to respond complexity:low Localized change in one scope with a small, straightforward review surface and removed community-request Issue reported or requested by someone from the community labels Sep 14, 2026
Combine llama.cpp overflow detection with the upstream propagation option in both API paths. Extend regression coverage for default length completions and original-error propagation.

Signed-off-by: wefstratis <[email protected]>
@wefstratis-nv

Copy link
Copy Markdown
Contributor Author

thanks @wefstratis-nv for the contribution! could you address the merge conflicts with the latest main so we can merge?

Yes this should be fixed now

@nemo-automation-bot nemo-automation-bot Bot added the community-request Issue reported or requested by someone from the community label Sep 16, 2026
@yaoyu-33 yaoyu-33 removed the community-request Issue reported or requested by someone from the community label Sep 16, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-customer Waiting on the original author to respond label Sep 16, 2026
@yaoyu-33 yaoyu-33 added the needs-review PR is ready for code review and waiting on a reviewer label Sep 16, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Sep 18, 2026
@yaoyu-33 yaoyu-33 removed the needs-review PR is ready for code review and waiting on a reviewer label Sep 18, 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.

Resolved the conflicts with current main, preserving engine-error logging, execution metadata, and Responses API coverage. llama.cpp overflow handling remains scoped to HTTP 400; unrelated errors still propagate.

Validation: 537 adapter/core regressions passed; the standard Gym component command passed all 404 component tests (overlapping the 537). Full pre-commit and the PR diff whitespace check passed. No fresh live-model rollout or full CI run was performed.

@yaoyu-33 yaoyu-33 added ready-to-merge PR is approved, current, and waiting for final CI or merge and removed waiting-on-maintainers Waiting on maintainers to respond labels Oct 8, 2026
@zyzhou5

zyzhou5 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

/ok to test 584fb00

@nemo-automation-bot nemo-automation-bot Bot added the community-request Issue reported or requested by someone from the community label Oct 8, 2026
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 8, 2026 19:27
@yaoyu-33 yaoyu-33 removed the community-request Issue reported or requested by someone from the community label Oct 8, 2026
@zyzhou5
zyzhou5 added this pull request to the merge queue Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/37846844856

Merged via the queue into NVIDIA-NeMo:main with commit 61cc9b1 Oct 8, 2026
43 checks passed
khazic pushed a commit to khazic/Gym-lao that referenced this pull request Oct 15, 2026
…Mo#3978)

## What does this PR do?

With `propagate_context_overflow_errors` on, the vLLM model server lets
the engine's HTTP 400 reach the harness when a request no longer fits
the model's context window. On `main` the harness receives vLLM's raw
body, whose `code` is the integer 400. An OpenAI-compatible harness
(Codex, OpenCode) classifies a context overflow by the string code
`context_length_exceeded` inside OpenAI's `error` envelope, so it cannot
recognize the refusal and compact its history; on a streaming
`/v1/chat/completions` request that has passed its keepalive, the error
event carries the same integer code.

1. **OpenAI's error envelope for a propagated refusal.**
`VLLMModel._should_propagate_upstream_http_error` returns false for an
error the server marked as a propagated length refusal, so the shared
error handler (NVIDIA-NeMo#4285) lets it through to the server's own middleware,
which answers `{"error": {"message": <the engine's message, verbatim>,
"type": "invalid_request_error", "param": <the engine's param>, "code":
...}}` with the engine's status. `context_overflow_error_fields` in
`nemo_gym.base_responses_api_model` reads the message and `param` from
vLLM's nested `error` object (0.10.1 and later), from the flat top-level
`message` of older engines, or from the raw text, so no information is
lost. The mark is one attribute, `PROPAGATED_REFUSAL_CODE_ATTRIBUTE`:
its presence marks the error (`is_propagated_refusal`) and its value is
the envelope code. A streaming `/v1/responses` request gets the same
HTTP 400 before the stream, as `main` answers it since NVIDIA-NeMo#4285; nothing
has been sent yet, so an HTTP status is the right answer and the one
OpenAI itself gives.
2. **The code says whether compacting the prompt is the remedy.** A body
that names the context window carries `code: "context_length_exceeded"`:
vLLM 0.29.0's two wordings ("This model's maximum context length is ..."
and "Input length (N) exceeds model's maximum context length (M)."), the
engine's "decoder prompt ... is longer than the maximum model length",
and llama.cpp's `exceed_context_size_error` type and phrases. A body
that only names the output-token parameter ("max_tokens must be at least
1", "max_completion_tokens=N cannot be greater than max_model_len")
carries `code: null`: lowering the parameter fixes those, shortening the
prompt does not, so the harness reports an ordinary invalid request with
the engine's sentence instead of compacting. The classification itself
stays the shared `_is_context_length_error`; it now also recognizes
`max_completion_tokens` and "maximum model length", two length refusals
that the f128d43 base answered with a plain HTTP 400 from the shared
handler. With the setting off they are now absorbed into an empty
completion with `finish_reason: "length"` like every other recognized
refusal (see below); with it on they carry the envelope,
`context_length_exceeded` for the "maximum model length" wording and
`code: null` for the `max_completion_tokens` parameter error.
3. **The chat-SSE error after the keepalive carries the envelope.** Once
a streaming `/v1/chat/completions` request has committed its headers, a
marked refusal is reported as `event: error` with the same message,
`param`, and code, and logged as one warning instead of a traceback.

The default (`propagate_context_overflow_errors: false`) keeps absorbing
a recognized refusal into an empty HTTP 200 completion with
`finish_reason: "length"`. One consequence of the wider classifier: the
`max_completion_tokens` and "maximum model length" phrasings are now
recognized too, so with propagation off they are absorbed the same way,
where before this change they fell through to the shared HTTP 400
handler. With propagation on, a parameter-only error keeps `code: null`
and a context-window error carries `context_length_exceeded`.

### Relation to the model-execution record

`ModelExecutionOutcome` records an absorbed overflow as
`local_response_reason: "context_length_exceeded"` on the observability
model-call record, and that path is unchanged here. This PR concerns the
propagated case only, which is what the client sees.

## Issue

No issue filed; observed while training with the Codex CLI through the
streaming `/v1/responses` route, which could not classify the overflow
and retried the request.

## Validation

- `responses_api_models/vllm_model/tests/test_app.py`,
`tests/unit_tests/test_base_responses_api_model.py`,
`tests/unit_tests/test_responses_api_model_streaming.py`,
`tests/unit_tests/test_chat_completions_streaming.py`: 659 passed. The
propagation test runs six engine bodies (vLLM 0.29.0's two overflow
wordings with their `param`, the engine's "maximum model length"
message, llama.cpp's typed refusal, and the two output-token parameter
errors) on both engine routes and both APIs, with the setting on and
off; new tests cover the HTTP 400 on a streaming `/v1/responses`
request, the envelope in the chat-SSE error after the keepalive, and the
message, `param`, and code extraction from the nested, flat, and raw
bodies.
- Full `tests/unit_tests` and every `responses_api_models/*/tests` suite
on this branch: the only failures (opensandbox, telemetry, and the two
servers that import `vllm`) are the same as on `main` f128d43 in the
same environment.
- `ruff check` and `ruff format --check` on the changed files: clean.
- Rollout evidence: the envelope has run for weeks in a downstream fork
training with OpenCode and Codex harnesses under token-id capture with
`propagate_context_overflow_errors: true`. With it, Codex fails the turn
without an item and OpenCode records the error and exits, so a session
that reaches the window ends cleanly and trains up to its last completed
call instead of retrying the refused request.

### Review follow-up

Rebased onto `main` f128d43 after NVIDIA-NeMo#3371 (the shared
`_is_context_length_error`) and NVIDIA-NeMo#4285 (the shared error handler and the
HTTP 400 before a stream), which changed the shape of this change: the
envelope is produced through the propagation hook, and the terminal
`response.failed` event of the earlier revision is gone. The
`huggingface-hub<2` bound that the validation merges had added to this
branch lives in NVIDIA-NeMo#4362 now.

`main` 4518db9 is merged into the branch (c19c51c, 2026-10-14). The
one conflict was the FastAPI import line of
`responses_api_models/vllm_model/app.py`; both sides are kept, and
`Response` is dropped from the import because the envelope is returned
as a `JSONResponse`, so nothing in the module constructs a bare
`Response` any more.

yaoyu-33's suggestion of 2026-10-12 (00ad0f0): the boolean marker and
the code attribute are one attribute whose presence is the mark and
whose value is the code (`None` for an output-token parameter refusal
stays distinct from an unmarked exception); the marking helper, the
shared-handler hook, the HTTP middleware and the chat-SSE path read it,
and the extraction test covers the unmarked, `None`-code and
overflow-code states.
`tests/unit_tests/test_base_responses_api_model.py` 106 passed,
`responses_api_models/vllm_model/tests` 439 passed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: alexandery <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 584fb009 Deployed Oct 8, 2026 by copy-pr-bot[bot] via release / finalize / notify #4777
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters bug Something isn't working complexity:low Localized change in one scope with a small, straightforward review surface 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.

5 participants