fix: support remote Ollama server for function calling and pydantic output (#4694)#4696
Closed
devin-ai-integration[bot] wants to merge 2 commits into
Closed
fix: support remote Ollama server for function calling and pydantic output (#4694)#4696devin-ai-integration[bot] wants to merge 2 commits into
devin-ai-integration[bot] wants to merge 2 commits into
Conversation
…utput Bug 1: InternalInstructor.to_pydantic() now forwards api_base/base_url/api_key to litellm so that remote Ollama servers are reachable during structured output parsing. Bug 2: LLM.supports_function_calling() now falls back to querying the remote Ollama /api/show endpoint when litellm returns False for Ollama models with a non-localhost base URL. Fixes #4694 Co-Authored-By: João <joao@crewai.com>
Contributor
Author
|
Prompt hidden (unlisted session) |
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
…port/var - Guard _get_llm_extra_kwargs with is_litellm check to avoid passing litellm-specific kwargs to non-litellm instructor clients - Remove unused pytest import - Remove unused result variable in test_to_pydantic_forwards_api_key - Add test for non-litellm path returning empty kwargs Co-Authored-By: João <joao@crewai.com>
Contributor
|
Closing as stale — no activity in 30+ days. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: support remote Ollama server for function calling and pydantic output
Fixes #4694
Summary
Two bugs prevented CrewAI from working with remote (non-localhost) Ollama servers:
Bug 1 —
InternalInstructor.to_pydantic()loses remote URL: The instructor's completion call to litellm did not forwardapi_base/base_url/api_keyfrom the LLM object, so litellm defaulted to localhost and failed to reach the remote Ollama server during structured output parsing. Fixed by adding_get_llm_extra_kwargs()which extracts these params and splats them into thecreate()call.Bug 2 —
LLM.supports_function_calling()always returns False for remote Ollama:litellm.utils.supports_function_calling()internally tries to querylocalhost:11434to check model capabilities, which fails when Ollama runs on a different host. Fixed by adding a fallback that queries the remote Ollama/api/showendpoint directly, inspectingmodel_infokeys and the template string for tool support indicators.Files changed
lib/crewai/src/crewai/utilities/internal_instructor.py— new_get_llm_extra_kwargs()helper + forwarding into_pydantic()lib/crewai/src/crewai/llm.py— new_is_ollama_model(),_get_ollama_base_url(),_check_ollama_function_calling()helpers + fallback insupports_function_calling()lib/crewai/tests/test_remote_ollama.py— 31 new unit tests covering both bugsReview & Testing Checklist for Human
_check_ollama_function_calling()checks for"tool" in key.lower() and value is Trueinmodel_infoand"tools"/".ToolCalls"in the template string. Confirm these patterns match actual/api/showresponses for tool-capable models (e.g.,mistral-small3.2,qwen2.5) and don't false-positive on non-tool models._check_ollama_function_calling()makes a blockinghttpx.postwith 5s timeout. This only fires when litellm returns False for an Ollama model with a remote URL, but verify this doesn't introduce unacceptable latency in your workflows.LLM.supports_function_calling()returns True for tool-capable models likeollama_chat/mistral-small3.2:24bInternalInstructor.to_pydantic()successfully parses structured output from a remote Ollama modelollama/llama2) correctly return Falseapi_base(should be stripped), unreachable servers (should return False without crashing), and localhost URLs (should behave as before).Notes
object.__new__()to bypassInternalInstructor.__init__and avoid the dynamic instructor import; this doesn't test the real initialization path but allows testing the helper methods in isolation.pytestimport can be removed if desired (no parametrize or raises used).Note
Medium Risk
Adds a new synchronous HTTP capability probe to
LLM.supports_function_calling()for remote Ollama and changes instructor completion kwargs forwarding; risk is moderate due to potential latency/false positives and altered request parameters.Overview
Fixes remote Ollama usage by ensuring structured-output (
InternalInstructor.to_pydantic) forwards provider connection kwargs (api_base,base_url,api_key) into the litellmcreate()call.Adds an Ollama-specific fallback in
LLM.supports_function_calling()that, when litellm reports no tool support and a remote URL is configured, queries the remote Ollama/api/showendpoint to infer tool/function-calling capability.Introduces a new
test_remote_ollama.pysuite covering both behaviors (kwargs forwarding and remote capability probing) with mockedhttpx/litellm interactions.Written by Cursor Bugbot for commit 9bdc7b9. This will update automatically on new commits. Configure here.