Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Sep 11, 2025

This PR fixes an issue where the Ollama API key was not being included in the Authorization header when making requests to /api/tags and /api/show endpoints.

Problem

When using a custom Ollama URL with an API key configured, the API key was not being sent in the Authorization header for requests to fetch available models. This prevented authentication with Ollama cloud or other authenticated Ollama instances.

Solution

  • Updated GetModelsOptions type to include an optional apiKey field for the ollama provider
  • Modified getOllamaModels function to accept and use the API key parameter
  • Added Authorization header with Bearer token when API key is provided
  • Updated all callers to pass the API key when available:
    • webviewMessageHandler for UI model fetching
    • modelCache for cached model fetching
    • native-ollama provider for model fetching

Testing

  • Added comprehensive test coverage for API key authentication
  • All existing tests continue to pass
  • Verified that requests include proper Authorization headers when API key is configured

Fixes #7902


Important

Fixes missing API key in Ollama requests by updating getOllamaModels() to include Authorization header when API key is provided.

  • Behavior:
    • Fixes missing API key in Authorization header for /api/tags and /api/show requests in getOllamaModels().
    • Updates GetModelsOptions to include optional apiKey for Ollama.
    • Modifies webviewMessageHandler and native-ollama to pass API key.
  • Functions:
    • getOllamaModels() in ollama.ts now accepts apiKey and includes it in request headers.
    • Updates fetchModel() in native-ollama.ts to use API key.
  • Testing:
    • Adds test in ollama.test.ts to verify Authorization header inclusion when API key is provided.
    • Ensures existing tests pass with new changes.

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

- Updated GetModelsOptions type to include optional apiKey for ollama provider
- Modified getOllamaModels function to accept and use apiKey parameter in Authorization headers
- Updated all callers (webviewMessageHandler, modelCache, native-ollama) to pass apiKey
- Added test coverage for API key authentication

Fixes #7902
@roomote roomote bot requested review from cte, jr and mrubens as code owners September 11, 2025 15:31
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Sep 11, 2025
Copy link
Contributor 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.

Review Summary

The implementation correctly addresses issue #7902 by adding API key support to Ollama requests. The changes are clean and follow existing patterns.

Strengths:

  • ✅ API key is properly included in Authorization headers for both /api/tags and /api/show endpoints
  • ✅ All callers updated to pass the API key when available
  • ✅ Type definitions correctly updated
  • ✅ Comprehensive test coverage for the new functionality

Suggestions for improvement:

  1. Error handling - Consider enhancing error handling in getOllamaModels() to distinguish between authentication failures (401/403) and connection errors. This would help users understand if their API key is invalid vs if Ollama is simply unreachable.

  2. Native client compatibility - In native-ollama.ts, verify that the ollama npm package actually supports and passes custom headers. The package documentation doesn't explicitly mention header support in the Config type.

  3. Test coverage - Consider adding tests for authentication failure scenarios to ensure proper error handling.

Overall, this is a solid implementation that solves the reported issue effectively.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Sep 11, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Sep 12, 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 Sep 12, 2025
Copy link
Member

@daniel-lxs daniel-lxs left a comment

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Sep 12, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Needs Review] in Roo Code Roadmap Sep 12, 2025
@mrubens mrubens merged commit b576dd9 into main Sep 14, 2025
29 checks passed
@mrubens mrubens deleted the fix/ollama-api-key-support branch September 14, 2025 02:55
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Sep 14, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer PR - Needs Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Ollama does not use apikey when using custom url

5 participants