Skip to content

fix: use llama.cpp context metadata for OpenAI-compatible models - #9531

Closed
he-yufeng wants to merge 1 commit into
aaif-goose:mainfrom
he-yufeng:fix/openai-compatible-meta-context-limit
Closed

fix: use llama.cpp context metadata for OpenAI-compatible models#9531
he-yufeng wants to merge 1 commit into
aaif-goose:mainfrom
he-yufeng:fix/openai-compatible-meta-context-limit

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Summary

  • read meta.n_ctx from OpenAI-compatible /models entries and surface it as ModelInfo.context_limit
  • keep the existing supported-model ID API behavior by deriving IDs from the parsed model metadata
  • preserve recommended-model filtering without dropping the provider-reported context limit

Why

Local llama.cpp-compatible servers can expose their runtime context window in /models as meta.n_ctx. Goose currently only keeps the model IDs from that response, so custom OpenAI-compatible local providers fall back to the default 128k context limit. That makes compaction/token-limit UI decisions too optimistic for servers running with smaller contexts.

To verify

  • cargo fmt --package goose
  • cargo test -p goose models_api --lib
  • cargo test -p goose openai_compatible --lib

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7a51e11728

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

}

fn context_limit_from_model_entry(model: &Value) -> Option<usize> {
let value = model.get("meta").and_then(|meta| meta.get("n_ctx"))?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Read llama.cpp's documented context field

For llama.cpp /v1/models, the documented meta object exposes the context size as n_ctx_train and does not include n_ctx. In that common response shape this helper returns None, so model_info_from_models_api_entry falls back to DEFAULT_CONTEXT_LIMIT instead of the advertised model limit; for local models with smaller windows, Goose can over-budget prompts and hit context errors despite the new metadata support.

Useful? React with 👍 / 👎.

@FredApps

Copy link
Copy Markdown

Definitely have this problem, it does not read the context limit that is present in the API. I checked the code over, this is a legit fix. Recommend approval.

@he-yufeng
he-yufeng force-pushed the fix/openai-compatible-meta-context-limit branch from 7a51e11 to a7b4152 Compare June 4, 2026 05:54
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Rebased this branch onto current aaif-goose:main and resolved the provider conflict with the newer upstream streaming usage changes.

Validation:

cargo fmt --check

cargo test -p goose providers::openai_compatible --lib
# 13 passed; 0 failed

git diff --check

The test run emits existing warnings outside this PR's touched provider file (goose-mcp::subprocess, extension_manager, config::base, and platform extension tests); I left those untouched.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a7b4152269

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +88 to +92
fn model_info_from_models_api_entry(provider_name: &str, model: &Value) -> Option<ModelInfo> {
let name = model.get("id").and_then(|v| v.as_str())?;
let config = ModelConfig::new_or_fail(name)
.with_canonical_limits(provider_name)
.with_context_limit(context_limit_from_model_entry(model));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Apply metadata parsing to declarative OpenAI providers

For the local OpenAI-compatible providers this is meant to fix (for example the lmstudio and llama_swap declarative configs), this parser is never reached: non-HuggingFace engine: "openai" providers are registered with OpenAiProvider::from_custom_config in crates/goose/src/config/declarative_providers.rs:605-614, and OpenAiProvider::fetch_models_from_api still parses /models into Vec<String> only at crates/goose/src/providers/openai.rs:606-609. In those local llama.cpp-compatible environments, /v1/models metadata is still discarded and Goose continues to use the default/canonical context limit instead of the server-advertised limit.

Useful? React with 👍 / 👎.

@he-yufeng
he-yufeng force-pushed the fix/openai-compatible-meta-context-limit branch from a7b4152 to 0b932b9 Compare June 12, 2026 21:28
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Rebased this onto current origin/main and resolved the provider import conflict from the recent goose_providers split, while keeping the llama.cpp meta.n_ctx context-limit handling intact.

Validated locally:

  • cargo fmt --check
  • cargo test -p goose providers::openai_compatible --lib (13 passed)
  • git diff --check origin/main..HEAD

CI is running again on the rebased head.

@DOsinga

DOsinga commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks so much for taking the time to put this together, @he-yufeng! 🙏

It turns out this overlaps heavily with #9530, which tackles the same underlying issue (#9529) — surfacing llama.cpp's context metadata from /v1/models so auto-compaction kicks in correctly for local servers. Since #9530 is already actively being iterated on (and is authored by the person who reported the issue), we're going to consolidate the effort there to avoid duplicate work.

One thing worth flagging for future reference: this PR patches OpenAiCompatibleProvider, but local llama.cpp / LM Studio / Ollama-style servers actually flow through OpenAiProvider (via from_custom_config in the declarative provider registration), so the new parsing path wouldn't be reached for those providers in practice. #9530 targets OpenAiProvider directly, which is why we're going with that one. There were also a couple of unaddressed codex review comments here (see https://github.com/aaif-goose/goose/blob/main/CONTRIBUTING.md#ai-code-reviews).

Going to close this as a duplicate of #9530, but please don't be discouraged — we'd love to have you contribute again, and jumping in on #9530's review thread is a great place to keep the momentum going. Thanks again! 🪿

@DOsinga DOsinga closed this Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants