I try to use crewAI with a local ollama instance running I manage to run my crew. The…
- User
I try to use crewAI with a local ollama instance running
I manage to run my crew. The first agent starts, want to read a file, the file is read by the file_reader tool, then the llm fails :
Traceback (most recent call last): response = base_llm_http_handler.completion( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ data = provider_config.transform_request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modified_prompt = ollama_pt(model=model, messages=messages) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tool_calls = messages[msg_i].get("tool_calls") ~~~~~~~~^^^^^^^ IndexError: list index out of range
- Assistant
Great job getting CrewAI running with a local Ollama instance! The error you're seeing:
IndexError: list index out of range tool_calls = messages[msg_i].get("tool_calls")suggests that the code is trying to access a message in the
messageslist that doesn't exist—likely because the list is empty ormsg_iis out of bounds.Why does this happen?
This is a common issue when integrating LLM frameworks (like CrewAI) with local LLM providers (like Ollama), especially when using tools or function-calling features. The root causes are usually:
- Unexpected message format: The messages list passed to Ollama is empty or not structured as expected.
- Mismatch in function-calling expectations: CrewAI expects the LLM to return tool calls in a certain way, but Ollama's response format may differ.
- Bug in the integration layer: The code that adapts CrewAI's prompt/messages to Ollama's API may have a bug.
How to Fix
1. Check the messages list before accessing
If you have access to the code, add a check before accessing
messages[msg_i]:if msg_i < len(messages): tool_calls = messages[msg_i].get("tool_calls") else: tool_calls = None2. Debug the messages content
Add a print/log statement before the error line to see what
messagescontains:print("MESSAGES:", messages) print("MSG_I:", msg_i)This will help you see if the list is empty or if
msg_iis out of range.3. Check your CrewAI and Ollama versions
- Make sure you are using the latest versions of both CrewAI and Ollama.
- Some earlier versions of CrewAI had issues with function-calling and tool integration with local LLMs.
4. Check your LLM model
- Not all Ollama models support function-calling or tool use. Try using
llama3,phi3, or another model known to support function-calling. - If your model doesn't support function-calling, CrewAI may still try to use it, leading to errors.
5. Update your CrewAI LLM config
If you are using a custom LLM integration, make sure your code handles empty or missing tool calls gracefully.
Example: Patch for CrewAI Ollama Integration
If you are using a custom provider, you might need to patch the code like this:
def ollama_pt(model, messages): # ... your code ... msg_i = ... # however you determine the index if msg_i < len(messages): tool_calls = messages[msg_i].get("tool_calls") else: tool_calls = None # ... rest of your code ...
TL;DR
- The error is because
messages[msg_i]is out of range. - Add a check to ensure
msg_iis within the bounds ofmessages. - Make sure your LLM and CrewAI versions are up to date.
- Ensure your Ollama model supports the features you need.
If you can share your CrewAI config and the code for your LLM provider, I can give you a more specific fix!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.