Skip to content

Commit 0ccae60

Browse files
committed
fix public key as constructor arg
1 parent 9ee2ef8 commit 0ccae60

5 files changed

Lines changed: 46 additions & 21 deletions

File tree

langfuse/_client/client.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ class Langfuse:
142142
```
143143
"""
144144

145+
_resources: Optional[LangfuseResourceManager] = None
146+
_mask: Optional[MaskFunction] = None
147+
145148
def __init__(
146149
self,
147150
*,
@@ -190,7 +193,6 @@ def __init__(
190193
langfuse_logger.warning(
191194
"Authentication error: Langfuse client initialized without public_key. Client will be disabled. "
192195
"Provide a public_key parameter or set LANGFUSE_PUBLIC_KEY environment variable. "
193-
"See documentation: https://langfuse.com/docs/sdk/python/low-level-sdk#initialize-client"
194196
)
195197
self._otel_tracer = otel_trace_api.NoOpTracer()
196198
return
@@ -200,7 +202,6 @@ def __init__(
200202
langfuse_logger.warning(
201203
"Authentication error: Langfuse client initialized without secret_key. Client will be disabled. "
202204
"Provide a secret_key parameter or set LANGFUSE_SECRET_KEY environment variable. "
203-
"See documentation: https://langfuse.com/docs/sdk/python/low-level-sdk#initialize-client"
204205
)
205206
self._otel_tracer = otel_trace_api.NoOpTracer()
206207
return
@@ -1326,7 +1327,9 @@ def create_score(
13261327
"timestamp": _get_timestamp(),
13271328
"body": new_body,
13281329
}
1329-
self._resources.add_score_task(event)
1330+
1331+
if self._resources is not None:
1332+
self._resources.add_score_task(event)
13301333

13311334
except Exception as e:
13321335
langfuse_logger.exception(
@@ -1520,7 +1523,8 @@ def flush(self):
15201523
# Continue with other work
15211524
```
15221525
"""
1523-
self._resources.flush()
1526+
if self._resources is not None:
1527+
self._resources.flush()
15241528

15251529
def shutdown(self):
15261530
"""Shut down the Langfuse client and flush all pending data.
@@ -1544,7 +1548,8 @@ def shutdown(self):
15441548
langfuse.shutdown()
15451549
```
15461550
"""
1547-
self._resources.shutdown()
1551+
if self._resources is not None:
1552+
self._resources.shutdown()
15481553

15491554
def get_current_trace_id(self) -> Optional[str]:
15501555
"""Get the trace ID of the current active span.
@@ -1926,6 +1931,10 @@ def get_prompt(
19261931
Exception: Propagates any exceptions raised during the fetching of a new prompt, unless there is an
19271932
expired prompt in the cache, in which case it logs a warning and returns the expired prompt.
19281933
"""
1934+
if self._resources is None:
1935+
raise Error(
1936+
"SDK is not correctly initalized. Check the init logs for more details."
1937+
)
19291938
if version is not None and label is not None:
19301939
raise ValueError("Cannot specify both version and label at the same time.")
19311940

@@ -2051,7 +2060,8 @@ def fetch_prompts():
20512060
else:
20522061
prompt = TextPromptClient(prompt_response)
20532062

2054-
self._resources.prompt_cache.set(cache_key, prompt, ttl_seconds)
2063+
if self._resources is not None:
2064+
self._resources.prompt_cache.set(cache_key, prompt, ttl_seconds)
20552065

20562066
return prompt
20572067

@@ -2150,7 +2160,8 @@ def create_prompt(
21502160
)
21512161
server_prompt = self.api.prompts.create(request=request)
21522162

2153-
self._resources.prompt_cache.invalidate(name)
2163+
if self._resources is not None:
2164+
self._resources.prompt_cache.invalidate(name)
21542165

21552166
return ChatPromptClient(prompt=cast(Prompt_Chat, server_prompt))
21562167

@@ -2169,7 +2180,8 @@ def create_prompt(
21692180

21702181
server_prompt = self.api.prompts.create(request=request)
21712182

2172-
self._resources.prompt_cache.invalidate(name)
2183+
if self._resources is not None:
2184+
self._resources.prompt_cache.invalidate(name)
21732185

21742186
return TextPromptClient(prompt=cast(Prompt_Text, server_prompt))
21752187

@@ -2200,7 +2212,9 @@ def update_prompt(
22002212
version=version,
22012213
new_labels=new_labels,
22022214
)
2203-
self._resources.prompt_cache.invalidate(name)
2215+
2216+
if self._resources is not None:
2217+
self._resources.prompt_cache.invalidate(name)
22042218

22052219
return updated_prompt
22062220

langfuse/_client/get_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ def get_client(*, public_key: Optional[str] = None) -> Langfuse:
5656

5757
if len(active_instances) == 1:
5858
# Only one client exists, safe to use without specifying key
59-
return Langfuse(public_key=public_key)
59+
instance = list(active_instances.values())[0]
60+
61+
# Initialize with the credentials bound to the instance
62+
# This is important if the original instance was instantiated
63+
# via constructor arguments
64+
return Langfuse(
65+
public_key=instance.public_key,
66+
secret_key=instance.secret_key,
67+
host=instance.host,
68+
)
6069

6170
else:
6271
# Multiple clients exist but no key specified - disable tracing

langfuse/_client/resource_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ def _initialize_instance(
136136
mask: Optional[MaskFunction] = None,
137137
):
138138
self.public_key = public_key
139+
self.secret_key = secret_key
140+
self.host = host
139141
self.mask = mask
140142

141143
# OTEL Tracer

langfuse/_client/span.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def _process_media_and_apply_mask(
410410
The processed and masked data
411411
"""
412412
return self._mask_attribute(
413-
data=self._process_media_in_attribute(data=data, span=span, field=field)
413+
data=self._process_media_in_attribute(data=data, field=field)
414414
)
415415

416416
def _mask_attribute(self, *, data):
@@ -441,7 +441,6 @@ def _process_media_in_attribute(
441441
self,
442442
*,
443443
data: Optional[Any] = None,
444-
span: otel_trace_api.Span,
445444
field: Union[Literal["input"], Literal["output"], Literal["metadata"]],
446445
):
447446
"""Process any media content in the attribute data.
@@ -457,16 +456,17 @@ def _process_media_in_attribute(
457456
Returns:
458457
The data with any media content processed
459458
"""
460-
media_processed_attribute = (
461-
self._langfuse_client._resources._media_manager._find_and_process_media(
462-
data=data,
463-
field=field,
464-
trace_id=self.trace_id,
465-
observation_id=self.id,
459+
if self._langfuse_client._resources is not None:
460+
return (
461+
self._langfuse_client._resources._media_manager._find_and_process_media(
462+
data=data,
463+
field=field,
464+
trace_id=self.trace_id,
465+
observation_id=self.id,
466+
)
466467
)
467-
)
468468

469-
return media_processed_attribute
469+
return data
470470

471471

472472
class LangfuseSpan(LangfuseSpanWrapper):

tests/test_otel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,7 @@ def mask_sensitive_data(data):
23552355
# Since _process_media_in_attribute makes calls to media_manager
23562356
original_process = span._process_media_in_attribute
23572357

2358-
def mock_process_media(*, data, span, field):
2358+
def mock_process_media(*, data, field):
23592359
# Just return the data directly without processing
23602360
return data
23612361

0 commit comments

Comments
 (0)