joaomdmoura / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
16.91k stars 2.28k forks source link

'generator' object does not support the context manager protocol #851

Open miagi-quyenhn opened 3 days ago

miagi-quyenhn commented 3 days ago

Currently I'm having an error when updating crewai from version 0.30.11 to 0.35.0 included with the packages (langchain-openai, embedchain). When running crew.kickoff(), the error 'generator' object does not support the context manager protocol will appear. If anyone has any suggestions to help me resolve this bug, please leave a comment. Thank you!

Environment information: crewai==0.30.11 => 0.35.0 crewai-tools==0.2.6 => 0.4.0 langchain-openai==0.0.5 => 0.1.7 embedchain==0.1.102 => 0.1.109

agent = Agent(
    role="xxxx",
    goal="xxxxx",
    backstory="xxxxxx",
    llm=self.llm,
    memory=True,
    verbose=True,
    allow_delegation=False,
)
task = Task(
    description="""
    xxxx
    """,
    expected_output="xxxx",
    agent=agent
)
crew = Crew(
    agents=agents,
    tasks=tasks,
    process=Process.sequential,
    verbose=2,
    memory=False
)
 self.logger.info('Preparing to kickoff crew')
try:
    self.logger.debug('Crew object: %s', crew)
    self.logger.debug('Crew kickoff method: %s', getattr(crew, 'kickoff', None))
    result = crew.kickoff(inputs={"title": title})
    self.logger.debug('Kickoff result type: %s', type(result))
except Exception as e:
    self.logger.exception("Error during crew kickoff")
    self.logger.info('Crew kickoff completed')

DEBUG LOG:

2024-07-03 11:18:19 - Preparing to kickoff crew
2024-07-03 11:18:19 - Crew object: cache=True tasks=[Task(description=For the Python program development task 'dfgdf\', please provide the following information at the basic design level.Please generate in Japanese.:  
....
> Entering new CrewAgentExecutor chain...
2024-07-03 11:18:19 - HTTP Request: POST https://cloud.getliteral.ai/api/graphql "HTTP/1.1 200 OK"
2024-07-03 11:18:20 - HTTP Request: POST https://cloud.getliteral.ai/api/graphql "HTTP/1.1 200 OK"
2024-07-03 11:18:20 - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
2024-07-03 11:18:21 - Error during crew kickoff
Traceback (most recent call last):
  File "D:\ideabox-project\BreakThroughNavigator\services\system_generator.py", line 473, in on_message
    result = crew.kickoff(inputs={"title": title})
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\crew.py", line 310, in kickoff
    result = self._run_sequential_process()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\crew.py", line 392, in _run_sequential_process
    output = task.execute(context=task_output)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\task.py", line 187, in execute
    result = self._execute(
             ^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\task.py", line 196, in _execute
    result = agent.execute_task(
             ^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\agent.py", line 162, in execute_task
    result = self.agent_executor.invoke(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain\chains\base.py", line 166, in invoke
    raise e
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain\chains\base.py", line 156, in invoke
    self._call(inputs, run_manager=run_manager)
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\agents\executor.py", line 71, in _call
    next_step_output = self._take_next_step(
                       ^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain\agents\agent.py", line 1139, in _take_next_step
    [
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\crewai\agents\executor.py", line 135, in _iter_next_step
    output = self.agent.plan(  # type: ignore #  Incompatible types in assignment (expression has type "AgentAction | AgentFinish | list[AgentAction]", variable has type "AgentAction")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain\agents\agent.py", line 398, in plan
    for chunk in self.runnable.stream(inputs, config={"callbacks": callbacks}):
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 2883, in stream
    yield from self.transform(iter([input]), config, **kwargs)
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 2870, in transform
    yield from self._transform_stream_with_config(
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 1868, in _transform_stream_with_config
    chunk: Output = context.run(next, iterator)  # type: ignore
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 2832, in _transform
    for output in final_pipeline:
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 1163, in transform
    for ichunk in input:
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 4797, in transform
    yield from self.bound.transform(
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 1181, in transform
    yield from self.stream(final, config, **kwargs)
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\language_models\chat_models.py", line 343, in stream
    raise e
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_core\language_models\chat_models.py", line 323, in stream
    for chunk in self._stream(messages, stop=stop, **kwargs):
  File "D:\ideabox-project\BreakThroughNavigator\.venv\Lib\site-packages\langchain_openai\chat_models\base.py", line 487, in _stream
    with self.client.create(**payload) as response:
TypeError: 'generator' object does not support the context manager protocol
2024-07-03 11:18:21 - Crew kickoff completed
theCyberTech commented 2 days ago

Can you try the latest version crewai-0.35.8, let us know if still an issue

miagi-quyenhn commented 2 days ago

Can you try the latest version crewai-0.35.8, let us know if still an issue

Hi @theCyberTech , I upgraded crewai to 0.35.8 but the error still occurs, maybe the problem lies in langchain-openai when using chainlit + crewAI application, do you have any suggestions for me?

image

miagi-quyenhn commented 2 days ago

It's agentops. That's the problem.

self.logger.debug('get api key.AgentOps')
agentops_api_key = os.environ['AGENTOPS_API_KEY']
self.logger.debug('AgentOps initialise')
agentops.init(agentops_api_key)
agentops.start_session()  
self.logger.info('crew kickoff start')
self.result = crew.kickoff()
self.logger.info('crew kickoff end')
self.logger.debug('AgentOps terminate')
agentops.end_session("Success")

Removing agentops helps eliminate this error!

robertobalestri commented 2 days ago

I'm having the same problem.. AgentOps doesn't work anymore with CrewAI, but i don't know if the problem is from AgentOps or from CrewAI. Please help because AgentOPS is essential

ItouTerukazu commented 1 day ago

I'm having the same problem..