feat(ollama): detect model capabilities dynamically via /api/show#5114
feat(ollama): detect model capabilities dynamically via /api/show#5114intel352 wants to merge 3 commits intogenkit-ai:mainfrom
Conversation
Replace the hardcoded toolSupportedModels and mediaSupportedModels allowlists with dynamic capability detection from Ollama's /api/show endpoint. This ensures newly released models (e.g. gemma4) work automatically without code changes. The /api/show response includes a "capabilities" array (e.g. ["completion", "vision", "tools", "thinking"]) that accurately reports what each model supports. This is more reliable than a static allowlist that requires manual updates for every new model. Fallback behavior: when /api/show doesn't return capabilities (older Ollama versions), the existing static allowlists are used as a fallback, preserving backward compatibility. Changes: - Add getModelCapabilities() to query /api/show for a model - Add modelSupportsFromCapabilities() to derive ModelSupports - Update DefineModel() to use dynamic detection - Update ListActions() and ResolveAction() to use dynamic detection - Add tests for capability detection and fallback behavior
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request replaces hardcoded model capability allowlists with dynamic capability detection by querying the Ollama /api/show endpoint. This allows newly released models to work automatically without code changes. Feedback focuses on performance and reliability concerns regarding network I/O, specifically recommending the use of contexts with timeouts to prevent hanging requests, checking for context cancellation in loops, and avoiding I/O operations while holding mutexes to prevent resource contention.
…lation check - DefineModel: detect capabilities before acquiring o.mu to avoid blocking the lock on HTTP I/O; use context.WithTimeout instead of context.Background() - ListActions: check ctx.Err() before each sequential /api/show call so a cancelled context stops the loop immediately - ResolveAction: use context.WithTimeout(o.Timeout) instead of bare context.Background() so the capability query is bounded
The previous fix moved I/O outside the lock but left the initted guard after it, so a missing Init() would hang for Timeout seconds. Now: 1. Briefly lock, check o.initted, unlock immediately (panic if not init'd) 2. Do HTTP capability detection without holding the lock 3. Re-acquire lock for model registration
30edb68 to
26ef251
Compare
Summary
Replace the hardcoded
toolSupportedModelsandmediaSupportedModelsallowlists in the Ollama plugin with dynamic capability detection from Ollama's/api/showendpoint.Problem
The Ollama plugin maintains static string arrays of model names that support tools and media. Newly released models (e.g.
gemma4, which Ollama reports as tool-capable via its API) don't work with tool calling until someone manually updates these lists and ships a new Genkit release. This creates an unnecessary maintenance burden and blocks users from using new models with tools.Solution
Ollama's
POST /api/showendpoint returns acapabilitiesarray (e.g.["completion", "vision", "tools", "thinking"]) that accurately reports what each model supports. This PR queries that endpoint when determining model capabilities, falling back to the existing static lists when the server doesn't report capabilities (older Ollama versions).Changes
go/plugins/ollama/ollama.go:getModelCapabilities()— queries/api/showfor a model's capabilitiesmodelSupportsFromCapabilities()— derivesModelSupportsfrom capability strings, with static-list fallbackDefineModel()to use dynamic detection when no explicit opts providedListActions()andResolveAction()to use dynamic detectiongo/plugins/ollama/ollama_test.go:TestGetModelCapabilities— mock server tests for tools/no-tools/unknown modelsTestModelSupportsFromCapabilities— unit tests for dynamic and fallback pathsBackward Compatibility
/api/showreturns capabilities → used directly (new behavior)/api/showdoesn't return capabilities or fails → falls back to existing static allowlists (existing behavior preserved)ModelOptionspassed toDefineModel()are still respected (no change)Testing
All existing tests pass. New tests cover:
/api/showendpoint🤖 Generated with Claude Code