Is it possible to create a Hierarchical crew manager using Gemini and without LangChain?

Hi!

I am trying to create a hierarchical crew with a manager that uses Gemini.

I have already create a sequential crew with Gemini, but when I tried to change to a hierarchical crew, it stop working.

I think this is the most important part of the code (let me know if you want more snippets):

    @crew
    def crew(self) -> Crew:
        manager = Agent(
            config=self.agents_config['clinical_assistant'],
            verbose=True,
            allow_delegation=True,
        )

        return Crew(
            manager_agent=manager,
            agents=self.agents,
            tasks=self.tasks, 
            process=Process.hierarchical,
            verbose=True,
            planning=True,
        )

When I try to run the code, it shows the folloing error:

ERROR:root:LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
ERROR:root:LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
ERROR:root:LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 609, in completion
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 522, in completion
    openai_client: OpenAI = self._get_openai_client(  # type: ignore
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 333, in _get_openai_client
    _new_client = OpenAI(
                  ^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/openai/_client.py", line 110, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 1595, in completion
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 1568, in completion
    response = openai_chat_completions.completion(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 619, in completion
    raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agent.py", line 345, in execute_task
    result = self.agent_executor.invoke(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 102, in invoke
    formatted_answer = self._invoke_loop()
                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 193, in _invoke_loop
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 115, in _invoke_loop
    answer = self.llm.call(
             ^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/llm.py", line 164, in call
    response = litellm.completion(**params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/utils.py", line 999, in wrapper
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/utils.py", line 880, in wrapper
    result = original_function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 2951, in completion
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2148, in exception_type
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 314, in exception_type
    raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 609, in completion
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 522, in completion
    openai_client: OpenAI = self._get_openai_client(  # type: ignore
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 333, in _get_openai_client
    _new_client = OpenAI(
                  ^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/openai/_client.py", line 110, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 1595, in completion
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 1568, in completion
    response = openai_chat_completions.completion(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 619, in completion
    raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agent.py", line 345, in execute_task
    result = self.agent_executor.invoke(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 102, in invoke
    formatted_answer = self._invoke_loop()
                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 193, in _invoke_loop
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 115, in _invoke_loop
    answer = self.llm.call(
             ^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/llm.py", line 164, in call
    response = litellm.completion(**params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/utils.py", line 999, in wrapper
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/utils.py", line 880, in wrapper
    result = original_function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 2951, in completion
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2148, in exception_type
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 314, in exception_type
    raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 609, in completion
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 522, in completion
    openai_client: OpenAI = self._get_openai_client(  # type: ignore
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 333, in _get_openai_client
    _new_client = OpenAI(
                  ^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/openai/_client.py", line 110, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 1595, in completion
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 1568, in completion
    response = openai_chat_completions.completion(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/llms/openai/openai.py", line 619, in completion
    raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/bin/run_crew", line 8, in <module>
    sys.exit(run())
             ^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/src/genial_agents/main.py", line 21, in run
    GenialAgents().crew().kickoff(inputs=inputs)
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/crew.py", line 548, in kickoff
    self._handle_crew_planning()
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/crew.py", line 624, in _handle_crew_planning
    )._handle_crew_planning()
      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/utilities/planning_handler.py", line 39, in _handle_crew_planning
    result = planner_task.execute_sync()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/task.py", line 192, in execute_sync
    return self._execute_core(agent, context, tools)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/task.py", line 250, in _execute_core
    result = agent.execute_task(
             ^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agent.py", line 357, in execute_task
    result = self.execute_task(task, context, tools)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agent.py", line 357, in execute_task
    result = self.execute_task(task, context, tools)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agent.py", line 356, in execute_task
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agent.py", line 345, in execute_task
    result = self.agent_executor.invoke(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 102, in invoke
    formatted_answer = self._invoke_loop()
                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 193, in _invoke_loop
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 115, in _invoke_loop
    answer = self.llm.call(
             ^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/crewai/llm.py", line 164, in call
    response = litellm.completion(**params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/utils.py", line 999, in wrapper
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/utils.py", line 880, in wrapper
    result = original_function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/main.py", line 2951, in completion
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2148, in exception_type
    raise e
  File "/Users/regishattori/workspace/genial/genial_agents/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 314, in exception_type
    raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
An error occurred while running the crew: Command '['uv', 'run', 'run_crew']' returned non-zero exit status 1.

The log shows that i have no OpenAI API Key but I have already have the Gemini API Key defined in .env and that was working when using Sequential Crew.

How can I solve this?

I am using the 0.95.0 version.

I just figured something out.

If I remove planning=True, it works!

From what I understand, it seems like this feature might not be supported in Gemini. Does it have something to do with ‘deep thought’ that some OpenAI models have?

You may have to set the planning_llm manually. Give this a try (replace the settings with your model/config):

Crew(
...
planning_llm=LLM(
  model="gemini/gemini-1.5-pro-002",
  api_key=GEMINI_API_KEY,
  temperature=0,
)
)

CrewAI docs has the planning_llm set up slightly different in their example, using the ChatOpenAI class langchain_openai for their LLM.
Source: Planning - CrewAI
If the first option doesn’t work, another idea would be to try a modified version of the doc example, instead using the ChatGoogleGenerativeAI class from langchain_google_genai. Note that you may need to add API keys/required values as necessary into this class.

Source:
ChatGoogleGenerativeAI — 🦜🔗 LangChain documentation.

Example:

from langchain_google_genai import ChatGoogleGenerativeAI
...
...
Crew(
    ...
    planning_llm=ChatGoogleGenerativeAI(model="gemini-1.5-pro")
)

I hadn’t noticed this parameter!

Now it is working!

Thanks!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.