Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Callable,
)


from llama_index.core.async_utils import asyncio_run
import llama_index.core.instrumentation as instrument
from llama_index.core.base.llms.generic_utils import (
chat_to_completion_decorator,
Expand Down Expand Up @@ -309,7 +309,7 @@ def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any):
**kwargs.pop("generation_config", {}),
}
params = {**kwargs, "generation_config": generation_config}
next_msg, chat_kwargs = asyncio.run(
next_msg, chat_kwargs = asyncio_run(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure if this will solve the issue or not.

I think Ideally, we remove the need for asyncio.run completely from the sync code-path

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am writing in detail how we encountered this issue:

Steps to reproduce:

  • Call the GoogleGenAI.structured_predict from an asynchronous method

Now there could be two possible solutions:

  1. Completely unwrap the asyncio.run and create synchronous alternatives for whatever is inside.
  2. Remove all synchronous methods and just replace synchronous method = asyncio_run(asynchronous method)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should duplicate the method into a sync and async version, and avoid any calls to asyncio.run or asyncio_run

prepare_chat_params(
self.model, messages, self.use_file_api, self._client, **params
)
Expand All @@ -320,7 +320,7 @@ def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any):
)

if self.use_file_api:
asyncio.run(
asyncio_run(
delete_uploaded_files([*chat_kwargs["history"], next_msg], self._client)
)

Expand Down Expand Up @@ -366,7 +366,7 @@ def _stream_chat(
**kwargs.pop("generation_config", {}),
}
params = {**kwargs, "generation_config": generation_config}
next_msg, chat_kwargs = asyncio.run(
next_msg, chat_kwargs = asyncio_run(
prepare_chat_params(
self.model, messages, self.use_file_api, self._client, **params
)
Expand Down Expand Up @@ -405,7 +405,7 @@ def gen() -> ChatResponseGen:
yield llama_resp

if self.use_file_api:
asyncio.run(
asyncio_run(
delete_uploaded_files(
[*chat_kwargs["history"], next_msg], self._client
)
Expand Down Expand Up @@ -594,7 +594,7 @@ def structured_predict_without_function_calling(

messages = prompt.format_messages(**prompt_args)
contents = [
asyncio.run(
asyncio_run(
chat_message_to_gemini(message, self.use_file_api, self._client)
)
for message in messages
Expand All @@ -614,7 +614,7 @@ def structured_predict_without_function_calling(
)

if self.use_file_api:
asyncio.run(delete_uploaded_files(contents, self._client))
asyncio_run(delete_uploaded_files(contents, self._client))

if isinstance(response.parsed, BaseModel):
return response.parsed
Expand Down Expand Up @@ -644,7 +644,7 @@ def structured_predict(

messages = prompt.format_messages(**prompt_args)
contents = [
asyncio.run(
asyncio_run(
chat_message_to_gemini(message, self.use_file_api, self._client)
)
for message in messages
Expand All @@ -656,7 +656,7 @@ def structured_predict(
)

if self.use_file_api:
asyncio.run(delete_uploaded_files(contents, self._client))
asyncio_run(delete_uploaded_files(contents, self._client))

if isinstance(response.parsed, BaseModel):
return response.parsed
Expand Down Expand Up @@ -738,7 +738,7 @@ def stream_structured_predict(

messages = prompt.format_messages(**prompt_args)
contents = [
asyncio.run(
asyncio_run(
chat_message_to_gemini(message, self.use_file_api, self._client)
)
for message in messages
Expand Down Expand Up @@ -767,7 +767,7 @@ def gen() -> Generator[Union[Model, FlexibleModel], None, None]:
yield streaming_model

if self.use_file_api:
asyncio.run(delete_uploaded_files(contents, self._client))
asyncio_run(delete_uploaded_files(contents, self._client))

return gen()
else:
Expand Down
Loading