model, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider(
^^^^^^^^^^^^^^^^^
raise e
.BadRequestError:
suggests that CrewAI is trying to use its built-in LLM provider logic, which expects an OpenAI-like API, but you want to use a local Ollama model via LangChain.
Why This Happens
If you don’t pass a LangChain LLM instance to your Agent, CrewAI tries to use its default provider (OpenAI, etc.), which fails if you don’t have an API key or if the provider isn’t set up.
If you pass the wrong argument or the LLM object isn’t compatible, you’ll get a similar error.
How to Fix
1. Make Sure You Pass the LangChain LLM Instance
When you create your Agent, you must pass the llm argument with your OllamaLLM instance:
python
from langchain_ollama import OllamaLLM
from crewai import Agent
llm = OllamaLLM(model="llama3") # or your preferred model
agent = Agent(
role="Researcher",
goal="Research something",
backstory="Expert in research.",
llm=llm, # <--- THIS IS CRITICAL
verbose=True
)
If you forget llm=llm, CrewAI will default to its own provider and throw the error you saw.
2. Do Not Set llm_provider or llm_model in Agent
If you use the llm argument, do not set llm_provider or llm_model in the agent. Only use llm=llm.
3. Example: Minimal Working CrewAI + Ollama Script
python
from crewai import Agent, Task, Crew
from langchain_ollama import OllamaLLM
# 1. Create the local LLM instance
llm = OllamaLLM(model="llama3") # or "mistral", etc.
# 2. Create agents with the LLM
researcher = Agent(
role="Researcher",
goal="Find the latest AI trends.",
backstory="Expert in AI research.",
llm=llm,
verbose=True
)
writer = Agent(
role="Writer",
goal="Write a summary of AI trends.",
backstory="Tech journalist.",
llm=llm,
verbose=True
)
# 3. Create tasks
research_task = Task(
description="List 3 current AI trends.",
expected_output="A bullet list of 3 trends.",
agent=researcher
)
write_task = Task(
description="Write a blog post based on the researcher's findings.",
expected_output="A 300-word blog post.",
agent=writer,
context=[research_task]
)
# 4. Create and run the crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
verbose=2
)
result = crew.kickoff()
print(result)
Summary Checklist
✅ Ollama is running (ollama serve)
✅ Model is pulled (ollama pull llama3)
✅ You use llm=OllamaLLM(...) in every Agent
✅ No llm_provider or llm_model in Agent
✅ You have langchain-ollama installed
If you follow this pattern, the .BadRequestError should disappear and CrewAI will use your local Ollama model!
If you still get errors, please paste your full agent/task/crew code and I’ll help debug!