Skip to content

Conversation

@roomote
Copy link

@roomote roomote bot commented Aug 17, 2025

Description

This PR fixes an issue where the native Ollama handler was overriding user-configured num_ctx values in their Ollama model configurations.

Problem

In version 3.25.16, when the Ollama integration was switched to use the native API instead of the OpenAI compatibility layer, the implementation started explicitly setting num_ctx to modelInfo.contextWindow. This overrode any custom num_ctx value that users had configured in their Ollama models.

For users who had specifically configured their models with smaller context sizes to fit within their available memory (e.g., setting num_ctx to 16384), this caused the models to use the full context window (e.g., 1048576), leading to out-of-memory errors.

Solution

This fix removes the explicit num_ctx setting from the options passed to Ollama, allowing the service to use the model's configured value as intended. This restores the behavior from version 3.25.15 and earlier.

Changes

  • Removed num_ctx: modelInfo.contextWindow from the options in both createMessage and completePrompt methods
  • Added tests to verify that num_ctx is not being set in the options passed to Ollama

Testing

  • All existing tests pass
  • Added new tests specifically to verify num_ctx is not overridden
  • Manually verified that Ollama now uses the model's configured context size

Fixes #7159


Important

Fixes issue where num_ctx was overridden in Ollama models, restoring user-configured values and preventing out-of-memory errors.

  • Behavior:
    • Removes num_ctx: modelInfo.contextWindow from createMessage and completePrompt in native-ollama.ts.
    • Restores behavior from version 3.25.15, allowing user-configured num_ctx values.
  • Testing:
    • Adds tests in native-ollama.spec.ts to verify num_ctx is not set in options for createMessage and completePrompt.
    • All existing tests pass, ensuring no regression.
  • Misc:

This description was created by Ellipsis for df38bfe. You can customize this summary. It will automatically update as commits are pushed.

The native Ollama handler was explicitly setting num_ctx to modelInfo.contextWindow,
which overrode any user-configured num_ctx value in their Ollama model configuration.
This caused issues for users who had specifically configured their models with custom
context sizes to fit within their available memory.

This fix removes the num_ctx override, allowing Ollama to use the model's configured
value as intended.

Fixes #7159
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 17, 2025 07:57
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Aug 17, 2025
Copy link
Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

Reviewing my own code because apparently I trust no one, not even myself.

stream: true,
options: {
num_ctx: modelInfo.contextWindow,
// Don't override num_ctx - let Ollama use the model's configured value
Copy link
Author

Choose a reason for hiding this comment

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

Good fix removing the num_ctx override. However, could we consider adding documentation about how the Ollama context configuration works with this handler? It might help future developers understand why we're not setting this value explicitly.

messages: [{ role: "user", content: prompt }],
stream: false,
options: {
// Don't override num_ctx - let Ollama use the model's configured value
Copy link
Author

Choose a reason for hiding this comment

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

These comments are identical in both methods. Is this intentional? We could consider extracting this behavior note to a class-level comment or constant to avoid duplication and make it clear this is a deliberate pattern across all Ollama API calls.


// Specifically check that num_ctx is not in the options
const callArgs = mockChat.mock.calls[0][0]
expect(callArgs.options).not.toHaveProperty("num_ctx")
Copy link
Author

Choose a reason for hiding this comment

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

Nice test coverage for verifying num_ctx isn't set! Could we also add an integration test that verifies the handler works correctly when Ollama models have different configured context sizes? This would help catch any future regressions.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 17, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Aug 18, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 18, 2025
@daniel-lxs daniel-lxs closed this Aug 22, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 22, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Aug 22, 2025
@daniel-lxs daniel-lxs deleted the fix/ollama-num-ctx-override branch August 22, 2025 22:10
@daniel-lxs
Copy link
Member

This fix is incomplete. It removes the num_ctx override (good) but leaves a display issue: Roo Code shows the model's default context window, not the user's configured num_ctx value.

The proper fix needs to parse the actual num_ctx from Ollama's parameters field to display the correct context size. See issue #7159 for details.

@hannesrudolph
Copy link
Collaborator

Related to: #7797

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

Labels

bug Something isn't working PR - Needs Preliminary Review size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Ollama model num_ctx is being ignored in the newest version

4 participants