Skip to content

fix: support remote Ollama server for function calling and pydantic output (#4694)#4696

Closed
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1772625832-fix-remote-ollama-url
Closed

fix: support remote Ollama server for function calling and pydantic output (#4694)#4696
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1772625832-fix-remote-ollama-url

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot commented Mar 4, 2026

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 forward api_base/base_url/api_key from 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 the create() call.

Bug 2 — LLM.supports_function_calling() always returns False for remote Ollama: litellm.utils.supports_function_calling() internally tries to query localhost:11434 to check model capabilities, which fails when Ollama runs on a different host. Fixed by adding a fallback that queries the remote Ollama /api/show endpoint directly, inspecting model_info keys 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 in to_pydantic()
  • lib/crewai/src/crewai/llm.py — new _is_ollama_model(), _get_ollama_base_url(), _check_ollama_function_calling() helpers + fallback in supports_function_calling()
  • lib/crewai/tests/test_remote_ollama.py — 31 new unit tests covering both bugs

Review & Testing Checklist for Human

  • Verify tool-detection heuristics against real Ollama responses: _check_ollama_function_calling() checks for "tool" in key.lower() and value is True in model_info and "tools"/".ToolCalls" in the template string. Confirm these patterns match actual /api/show responses for tool-capable models (e.g., mistral-small3.2, qwen2.5) and don't false-positive on non-tool models.
  • Synchronous HTTP call in hot path: _check_ollama_function_calling() makes a blocking httpx.post with 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.
  • Test with a real remote Ollama server: All tests use mocks. Spin up an Ollama server on a different host (or Docker container with exposed port) and verify:
    • LLM.supports_function_calling() returns True for tool-capable models like ollama_chat/mistral-small3.2:24b
    • InternalInstructor.to_pydantic() successfully parses structured output from a remote Ollama model
    • Non-tool models (e.g., ollama/llama2) correctly return False
  • Edge cases: Test with trailing slashes in api_base (should be stripped), unreachable servers (should return False without crashing), and localhost URLs (should behave as before).

Notes

  • Test file uses object.__new__() to bypass InternalInstructor.__init__ and avoid the dynamic instructor import; this doesn't test the real initialization path but allows testing the helper methods in isolation.
  • The unused pytest import can be removed if desired (no parametrize or raises used).
  • Session: https://app.devin.ai/sessions/1aecc957ee64482b984d1bb06283ac54
  • Requested by: João

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 litellm create() 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/show endpoint to infer tool/function-calling capability.

Introduces a new test_remote_ollama.py suite covering both behaviors (kwargs forwarding and remote capability probing) with mocked httpx/litellm interactions.

Written by Cursor Bugbot for commit 9bdc7b9. This will update automatically on new commits. Configure here.

…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>
@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

Prompt hidden (unlisted session)

@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Comment thread lib/crewai/tests/test_remote_ollama.py Fixed
Comment thread lib/crewai/tests/test_remote_ollama.py Fixed
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread lib/crewai/src/crewai/utilities/internal_instructor.py
…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>
@greysonlalonde
Copy link
Copy Markdown
Contributor

Closing as stale — no activity in 30+ days.

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.

[BUG] Remote Ollama url ignored by litellm

1 participant