Skip to content

Commit

Permalink
fix: thread-safe discovery API setup (Issues#327) (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumeet213 authored Oct 24, 2024
1 parent 3d91916 commit db8221a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions google/generativeai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import inspect
import dataclasses
import pathlib
import threading
from typing import Any, cast
from collections.abc import Sequence
import httplib2
Expand Down Expand Up @@ -64,6 +65,7 @@ def patch_colab_gce_credentials():
class FileServiceClient(glm.FileServiceClient):
def __init__(self, *args, **kwargs):
self._discovery_api = None
self._local = threading.local()
super().__init__(*args, **kwargs)

def _setup_discovery_api(self, metadata: dict | Sequence[tuple[str, str]] = ()):
Expand All @@ -83,7 +85,7 @@ def _setup_discovery_api(self, metadata: dict | Sequence[tuple[str, str]] = ()):
request.http.close()

discovery_doc = content.decode("utf-8")
self._discovery_api = googleapiclient.discovery.build_from_document(
self._local.discovery_api = googleapiclient.discovery.build_from_document(
discovery_doc, developerKey=api_key
)

Expand Down Expand Up @@ -115,7 +117,7 @@ def create_file(
filename=path, mimetype=mime_type, resumable=resumable
)

request = self._discovery_api.media().upload(body={"file": file}, media_body=media)
request = self._local.discovery_api.media().upload(body={"file": file}, media_body=media)
for key, value in metadata:
request.headers[key] = value
result = request.execute()
Expand Down

0 comments on commit db8221a

Please sign in to comment.