langchain-ai / langchain

šŸ¦œšŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.2k stars 15.44k forks source link

OpenAIAssistantRunnable: object Run can't be used in 'await' expression #27262

Open berrazuriz1 opened 1 month ago

berrazuriz1 commented 1 month ago

Checked other resources

Example Code

assistant_id = ASSITANTS[agent_name]

async_client = AsyncOpenAI()
agent = OpenAIAssistantRunnable(assistant_id=assistant_id, async_client=async_client, as_agent=True)

tools = [
    GetCodeByIdTool(db_manager=graph_manager, company_id=company_id),
    KeywordSearchTool(db_manager=graph_manager, company_id=company_id),
]

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=False,
    return_intermediate_steps=True,
    max_iterations=20,
)

async def stream_agent_response():
    with get_openai_callback() as cb:
          async for chunk in self.agent_executor.astream({"content": input}):
              for step_info in self.process_and_format_chunk(chunk=chunk, chat_room=chat_room):
                  if step_info is not None:
                      yield step_info.to_dict()

Error Message and Stack Trace (if applicable)

Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 244, in run_asgi result = await self.app(self.scope, self.asgi_receive, self.asgi_send) # type: ignore[func-returns-value] File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 70, in call return await self.app(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/channels/routing.py", line 62, in call return await application(scope, receive, send) File "/app/blar/authentication/websocket/WebSocketAuthMiddleware.py", line 50, in call return await self.app(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/channels/routing.py", line 132, in call return await application( File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 94, in app return await consumer(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 58, in call await await_many_dispatch( File "/usr/local/lib/python3.10/site-packages/channels/utils.py", line 50, in await_many_dispatch await dispatch(result) File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 73, in dispatch await handler(message) File "/usr/local/lib/python3.10/site-packages/channels/generic/websocket.py", line 203, in websocket_receive await self.receive(text_data=message["text"]) File "/app/blar/agents/consumers.py", line 103, in receive await self.blar_agent_message(text_data_json) File "/app/blar/agents/consumers.py", line 219, in blar_agent_message raise e File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 518, in thread_handler raise exc_info[1] File "/app/blar/agents/consumers.py", line 173, in blar_agent_message async for event in custom_agent.stream_agent_response( File "/app/blar/agents/custom_agents/agent.py", line 232, in stream_agent_response async for chunk in self.agent_executor.astream({"content": input}): File "/usr/local/lib/python3.10/site-packages/langchain/agents/agent.py", line 1810, in astream async for step in iterator: File "/usr/local/lib/python3.10/site-packages/langchain/agents/agent_iterator.py", line 266, in aiter async for chunk in self.agent_executor._aiter_next_step( File "/usr/local/lib/python3.10/site-packages/langchain/agents/agent.py", line 1500, in _aiter_next_step output = await self._action_agent.aplan( File "/usr/local/lib/python3.10/site-packages/langchain/agents/agent.py", line 503, in aplan async for chunk in self.runnable.astream( File "/usr/local/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 1018, in astream yield await self.ainvoke(input, config, **kwargs) File "/usr/local/lib/python3.10/site-packages/langchain/agents/openai_assistant/base.py", line 474, in ainvoke raise e File "/usr/local/lib/python3.10/site-packages/langchain/agents/openai_assistant/base.py", line 437, in ainvoke tool_outputs = await self._aparse_intermediate_steps( File "/usr/local/lib/python3.10/site-packages/langchain/agents/openai_assistant/base.py", line 618, in _aparse_intermediate_steps run = await self._wait_for_run(last_action.run_id, last_action.thread_id) TypeError: object Run can't be used in 'await' expression

Description

It runs function calling well, but when it gets to a AI message chunk it throws this

System Info

aiohappyeyeballs==2.4.3
aiohttp==3.10.9
aiosignal==1.3.1
amqp==5.2.0
annotated-types==0.7.0
anyio==4.6.0
appnope==0.1.4
asgiref==3.8.1
asttokens==2.4.1
attrs==24.2.0
autobahn==24.4.2
Automat==24.8.1
beautifulsoup4==4.12.3
billiard==4.2.1
black==24.10.0
blar-graph==1.1.5
CacheControl==0.14.0
cachetools==5.5.0
celery==5.4.0
certifi==2024.8.30
cffi==1.17.1
cfgv==3.4.0
channels==4.1.0
channels-redis==4.2.0
charset-normalizer==3.4.0
click==8.1.7
click-didyoumean==0.3.1
click-plugins==1.1.1
click-repl==0.3.0
comm==0.2.2
constantly==23.10.4
cron-descriptor==1.4.5
cryptography==43.0.1
daphne==4.1.2
dataclasses-json==0.6.7
debugpy==1.8.5
decorator==5.1.1
Deprecated==1.2.14
dirtyjson==1.0.8
distlib==0.3.9
distro==1.9.0
dj-database-url==2.2.0
Django==5.1.2
django-celery-beat==2.7.0
django-cors-headers==4.4.0
django-debug-toolbar==4.4.6
django-encrypted-model-fields==0.6.5
django-environ==0.11.2
django-filter==24.3
django-push-notifications==3.1.0
django-redis==5.4.0
django-timezone-field==7.0
djangorestframework==3.15.2
djangorestframework-api-key==3.0.0
djangorestframework-simplejwt==5.3.1
dpath==2.2.0
drf-jwt==1.19.2
executing==2.1.0
filelock==3.16.1
firebase-admin==6.5.0
frozenlist==1.4.1
fsspec==2024.9.0
gitdb==4.0.11
GitPython==3.1.43
google-api-core==2.21.0
google-api-python-client==2.149.0
google-auth==2.35.0
google-auth-httplib2==0.2.0
google-cloud-core==2.4.1
google-cloud-firestore==2.19.0
google-cloud-storage==2.18.2
google-crc32c==1.6.0
google-resumable-media==2.7.2
googleapis-common-protos==1.65.0
greenlet==3.1.1
grpcio==1.66.2
grpcio-status==1.66.2
gunicorn==22.0.0
h11==0.14.0
httpcore==1.0.6
httplib2==0.22.0
httpx==0.27.2
hyperlink==21.0.0
identify==2.6.1
idna==3.10
incremental==24.7.2
ipdb==0.13.13
ipykernel==6.29.5
ipython==8.28.0
isort==5.13.2
jedi==0.19.1
jiter==0.6.1
joblib==1.4.2
json5==0.9.25
jsonpatch==1.33
jsonpointer==3.0.0
jupyter_client==8.6.2
jupyter_core==5.7.2
kombu==5.4.2
langchain==0.3.3
langchain-community==0.3.2
langchain-core==0.3.10
langchain-experimental==0.3.2
langchain-text-splitters==0.3.0
langgraph==0.2.35
langgraph-checkpoint==2.0.1
langsmith==0.1.132
llama-index-agent-openai==0.2.9
llama-index-core==0.10.68.post1
llama-index-llms-openai==0.1.31
llama-index-packs-code-hierarchy-blar==0.1.10
llama-index-readers-file==0.1.33
marshmallow==3.22.0
matplotlib-inline==0.1.7
msgpack==1.1.0
multidict==6.1.0
mypy-extensions==1.0.0
neo4j==5.25.0
nest-asyncio==1.6.0
networkx==3.3
newrelic==9.13.0
nltk==3.9.1
nodeenv==1.9.1
numpy==1.26.4
openai==1.51.2
orjson==3.10.7
packaging==24.1
pandas==2.2.3
parso==0.8.4
pathspec==0.12.1
pexpect==4.9.0
pillow==10.4.0
platformdirs==4.3.6
pre-commit==3.8.0
prompt_toolkit==3.0.48
propcache==0.2.0
proto-plus==1.24.0
protobuf==5.28.2
psutil==6.0.0
psycopg2==2.9.9
ptyprocess==0.7.0
pure_eval==0.2.3
pyasn1==0.6.1
pyasn1_modules==0.4.1
pycparser==2.22
pydantic==2.9.2
pydantic-settings==2.5.2
pydantic_core==2.23.4
PyGithub==2.4.0
Pygments==2.18.0
PyJWT==2.9.0
PyNaCl==1.5.0
pyOpenSSL==24.2.1
pyparsing==3.1.4
pypdf==4.3.1
python-crontab==3.2.0
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
pytz==2024.2
PyYAML==6.0.2
pyzmq==26.1.1
redis==5.1.1
regex==2024.9.11
requests==2.32.3
requests-toolbelt==1.0.0
rsa==4.9
ruff==0.4.10
scipy==1.14.1
sentry-sdk==2.16.0
service-identity==24.1.0
six==1.16.0
slack_sdk==3.33.1
smmap==5.0.1
sniffio==1.3.1
soupsieve==2.6
SQLAlchemy==2.0.35
sqlparse==0.5.1
stack-data==0.6.3
stripe==10.12.0
striprtf==0.0.26
tenacity==8.5.0
tiktoken==0.8.0
timeout-decorator==0.5.0
tornado==6.4.1
tqdm==4.66.5
traitlets==5.14.3
tree-sitter==0.20.4
tree-sitter-languages==1.10.2
Twisted==24.7.0
txaio==23.1.1
typing-inspect==0.9.0
typing_extensions==4.12.2
tzdata==2024.2
uritemplate==4.1.1
urllib3==2.2.3
uvicorn==0.30.6
vine==5.1.0
virtualenv==20.26.6
wcwidth==0.2.13
websockets==12.0
wrapt==1.16.0
yarl==1.14.0
zope.interface==7.0.3
berrazuriz1 commented 1 month ago

It seems that the await on the _aparse_intermidiate_steps is causing the error

def _wait_for_run(self, run_id: str, thread_id: str) -> Any:
        in_progress = True
        while in_progress:
            run = self.client.beta.threads.runs.retrieve(run_id, thread_id=thread_id)
            in_progress = run.status in ("in_progress", "queued")
            if in_progress:
                sleep(self.check_every_ms / 1000)
        return run

async def _aparse_intermediate_steps(
    self, intermediate_steps: List[Tuple[OpenAIAssistantAction, str]]
) -> dict:
    last_action, last_output = intermediate_steps[-1]
    run = await self._wait_for_run(last_action.run_id, last_action.thread_id)
    required_tool_call_ids = set()
    if run.required_action:
        required_tool_call_ids = {
            tc.id for tc in run.required_action.submit_tool_outputs.tool_calls
        }
    tool_outputs = [
        {"output": str(output), "tool_call_id": action.tool_call_id}
        for action, output in intermediate_steps
        if action.tool_call_id in required_tool_call_ids
    ]
    submit_tool_outputs = {
        "tool_outputs": tool_outputs,
        "run_id": last_action.run_id,
        "thread_id": last_action.thread_id,
    }
    return submit_tool_outputs

I erase the await and runs well now