Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: list index out of range when using async stream with a tool #773

Closed
off6atomic opened this issue Jan 21, 2025 · 2 comments · Fixed by #778
Closed

IndexError: list index out of range when using async stream with a tool #773

off6atomic opened this issue Jan 21, 2025 · 2 comments · Fixed by #778
Assignees
Labels
bug Something isn't working mirascope

Comments

@off6atomic
Copy link
Contributor

off6atomic commented Jan 21, 2025

Description

Run the following code:

import asyncio

from dotenv import load_dotenv
from mirascope.core import BaseMessageParam, openai

load_dotenv()


def hello_world_tool():
    print("hello world")


@openai.call("gpt-4o-mini", stream=True, tools=[hello_world_tool]) 
async def recommend_book(genre: str) -> list[BaseMessageParam]:
    return [BaseMessageParam(role="user", content=f"Recommend a {genre} book")]


async def main():
    stream = await recommend_book("fantasy")
    async for chunk, tool in stream:
        if tool:
            tool.call()
        else:
            print(chunk.content, end="", flush=True)


if __name__ == "__main__":
    asyncio.run(main())

Get the following output:

I recommend "The Name of the Wind" by Patrick Rothfuss. It's the first book in the "Kingkiller Chronicle" series and follows the story of Kvothe, a gifted young man who grows up to be a legendary figure. The book is known for its beautiful prose, rich world-building, and deep character development. If you enjoy tales of magic, adventure, and music, this book is a fantastic choice!Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/chanchana/Documents/catalyst-ai/src/v2/scripts/test_mirascope.py", line 28, in <module>
    asyncio.run(main())
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/Users/chanchana/Documents/catalyst-ai/src/v2/scripts/test_mirascope.py", line 20, in main
    async for chunk, tool in stream:
  File "/Users/chanchana/Documents/catalyst-ai/.venv/lib/python3.10/site-packages/mirascope/core/openai/stream.py", line 93, in generator
    async for chunk, tool in aiter:
  File "/Users/chanchana/Documents/catalyst-ai/.venv/lib/python3.10/site-packages/mirascope/core/base/stream.py", line 171, in generator
    async for chunk, tool in self.stream:
  File "/Users/chanchana/Documents/catalyst-ai/.venv/lib/python3.10/site-packages/mirascope/core/base/stream.py", line 381, in generator
    async for chunk, tool in handle_stream_async(
  File "/Users/chanchana/Documents/catalyst-ai/.venv/lib/python3.10/site-packages/mirascope/core/openai/_utils/_handle_stream.py", line 121, in handle_stream_async
    if not tool_types or not chunk.choices[0].delta.tool_calls:
IndexError: list index out of range

If you remove the hello_world_tool from the tools argument the code would run without error.

How can I work around this?

Python, Mirascope & OS Versions, related packages (not required)

openai==1.59.7
pydantic==2.10.5
pydantic-core==2.27.2
mirascope==1.15.1
@off6atomic off6atomic added the bug Something isn't working label Jan 21, 2025
@off6atomic
Copy link
Contributor Author

off6atomic commented Jan 21, 2025

@willbakst This one is a serious bug I think. Because it disallow me from streaming tools and tokens asynchronously (for FastAPI endpoint). In v0, I was able to do it.

@willbakst willbakst self-assigned this Jan 21, 2025
@off6atomic off6atomic changed the title No stream response when adding tool to an async call IndexError: list index out of range when using async stream with a tool Jan 22, 2025
willbakst pushed a commit that referenced this issue Jan 24, 2025
@willbakst willbakst mentioned this issue Jan 24, 2025
@willbakst
Copy link
Contributor

Looks like the fix for this was already implemented in the sync case but missed for the async case.

Fix is released in v1.16.1!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mirascope
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants