Skip to content

Commit 994803a

Browse files
committed
[ENH] For chroma cloud efs, extract api key from header if available to authenticate
1 parent cbe2c4f commit 994803a

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

chromadb/api/shared_system_client.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import ClassVar, Dict
1+
from typing import ClassVar, Dict, Optional
22
import uuid
33

44
from chromadb.api import ServerAPI
@@ -94,3 +94,42 @@ def _system(self) -> System:
9494
def _submit_client_start_event(self) -> None:
9595
telemetry_client = self._system.instance(ProductTelemetryClient)
9696
telemetry_client.capture(ClientStartEvent())
97+
98+
@staticmethod
99+
def get_chroma_cloud_api_key_from_clients() -> Optional[str]:
100+
"""
101+
try to extract api key from existing client instaces by checking httpx session headers
102+
if available.
103+
104+
Returns:
105+
The first api key found, or None if no client instances have api keys set.
106+
"""
107+
# Check FastAPI instances' session headers - this is where both paths converge
108+
for system in SharedSystemClient._identifier_to_system.values():
109+
try:
110+
# Get the ServerAPI instance (which is FastAPI for HTTP clients)
111+
server_api = system.instance(ServerAPI)
112+
113+
# Check if it's a FastAPI instance with a _session attribute
114+
if hasattr(server_api, "_session") and hasattr(
115+
server_api._session, "headers"
116+
):
117+
if (
118+
hasattr(server_api, "_api_url")
119+
and "api.trychroma.com" not in server_api._api_url
120+
):
121+
continue
122+
123+
headers = server_api._session.headers
124+
api_key = headers.get("X-Chroma-Token") or headers.get(
125+
"x-chroma-token"
126+
)
127+
if api_key:
128+
# header value might be a string or bytes, convert to string
129+
return str(api_key)
130+
except Exception:
131+
# if we can't access the ServerAPI instance or it doesn't have _session,
132+
# continue to the next system instance
133+
continue
134+
135+
return None

chromadb/utils/embedding_functions/chroma_cloud_qwen_embedding_function.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,19 @@ def __init__(
5656
)
5757

5858
self.api_key_env_var = api_key_env_var
59+
# First, try to get API key from environment variable
5960
self.api_key = os.getenv(api_key_env_var)
61+
# If not found in env var, try to get it from existing client instances
6062
if not self.api_key:
61-
raise ValueError(f"The {api_key_env_var} environment variable is not set.")
63+
from chromadb.api.shared_system_client import SharedSystemClient
64+
65+
self.api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients()
66+
# Raise error if still no API key found
67+
if not self.api_key:
68+
raise ValueError(
69+
f"API key not found in environment variable {api_key_env_var} "
70+
f"or in any existing client instances"
71+
)
6272

6373
self.model = model
6474
self.task = task

chromadb/utils/embedding_functions/chroma_cloud_splade_embedding_function.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from enum import Enum
88
from chromadb.utils.embedding_functions.schemas import validate_config_schema
99
from chromadb.utils.sparse_embedding_utils import normalize_sparse_vector
10-
from chromadb.base_types import SparseVector
1110
import os
1211
from typing import Union
1312

@@ -36,10 +35,19 @@ def __init__(
3635
"The httpx python package is not installed. Please install it with `pip install httpx`"
3736
)
3837
self.api_key_env_var = api_key_env_var
38+
# First, try to get API key from environment variable
3939
self.api_key = os.getenv(self.api_key_env_var)
40+
# If not found in env var, try to get it from existing client instances
41+
if not self.api_key:
42+
# Import here to avoid circular import
43+
from chromadb.api.shared_system_client import SharedSystemClient
44+
45+
self.api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients()
46+
# Raise error if still no API key found
4047
if not self.api_key:
4148
raise ValueError(
42-
f"API key not found in environment variable {self.api_key_env_var}"
49+
f"API key not found in environment variable {self.api_key_env_var} "
50+
f"or in any existing client instances"
4351
)
4452
self.model = model
4553
self._api_url = "https://embed.trychroma.com/embed_sparse"

0 commit comments

Comments
 (0)