Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
LANGFUSE_PUBLIC_KEY,
LANGFUSE_SAMPLE_RATE,
LANGFUSE_SECRET_KEY,
LANGFUSE_TIMEOUT,
LANGFUSE_TRACING_ENABLED,
LANGFUSE_TRACING_ENVIRONMENT,
)
Expand Down Expand Up @@ -90,7 +91,7 @@ class Langfuse:
public_key (Optional[str]): Your Langfuse public API key. Can also be set via LANGFUSE_PUBLIC_KEY environment variable.
secret_key (Optional[str]): Your Langfuse secret API key. Can also be set via LANGFUSE_SECRET_KEY environment variable.
host (Optional[str]): The Langfuse API host URL. Defaults to "https://cloud.langfuse.com". Can also be set via LANGFUSE_HOST environment variable.
timeout (Optional[int]): Timeout in seconds for API requests. Defaults to 30 seconds.
timeout (Optional[int]): Timeout in seconds for API requests. Defaults to 5 seconds.
httpx_client (Optional[httpx.Client]): Custom httpx client for making non-tracing HTTP requests. If not provided, a default client will be created.
debug (bool): Enable debug logging. Defaults to False. Can also be set via LANGFUSE_DEBUG environment variable.
tracing_enabled (Optional[bool]): Enable or disable tracing. Defaults to True. Can also be set via LANGFUSE_TRACING_ENABLED environment variable.
Expand Down Expand Up @@ -168,6 +169,8 @@ def __init__(
f"Sample rate must be between 0.0 and 1.0, got {sample_rate}"
)

timeout = timeout or int(os.environ.get(LANGFUSE_TIMEOUT, 5))

self._tracing_enabled = (
tracing_enabled
and os.environ.get(LANGFUSE_TRACING_ENABLED, "True") != "False"
Expand Down Expand Up @@ -1840,7 +1843,7 @@ def resolve_media_references(
obj: Any,
resolve_with: Literal["base64_data_uri"],
max_depth: int = 10,
content_fetch_timeout_seconds: int = 10,
content_fetch_timeout_seconds: int = 5,
):
"""Replace media reference strings in an object with base64 data URIs.

Expand All @@ -1857,7 +1860,7 @@ def resolve_media_references(
resolve_with: The representation of the media content to replace the media reference string with.
Currently only "base64_data_uri" is supported.
max_depth: int: The maximum depth to traverse the object. Default is 10.
content_fetch_timeout_seconds: int: The timeout in seconds for fetching media content. Default is 10.
content_fetch_timeout_seconds: int: The timeout in seconds for fetching media content. Default is 5.

Returns:
A deep copy of the input object with all media references replaced with base64 data URIs where possible.
Expand Down Expand Up @@ -1947,7 +1950,7 @@ def get_prompt(
type: Literal["chat", "text"]: The type of the prompt to retrieve. Defaults to "text".
fallback: Union[Optional[List[ChatMessageDict]], Optional[str]]: The prompt string to return if fetching the prompt fails. Important on the first call where no cached prompt is available. Follows Langfuse prompt formatting with double curly braces for variables. Defaults to None.
max_retries: Optional[int]: The maximum number of retries in case of API/network errors. Defaults to 2. The maximum value is 4. Retries have an exponential backoff with a maximum delay of 10 seconds.
fetch_timeout_seconds: Optional[int]: The timeout in milliseconds for fetching the prompt. Defaults to the default timeout set on the SDK, which is 10 seconds per default.
fetch_timeout_seconds: Optional[int]: The timeout in milliseconds for fetching the prompt. Defaults to the default timeout set on the SDK, which is 5 seconds per default.

Returns:
The prompt object retrieved from the cache or directly fetched if not cached or expired of type
Expand Down Expand Up @@ -2066,7 +2069,7 @@ def _fetch_prompt_and_update_cache(
try:

@backoff.on_exception(
backoff.constant, Exception, max_tries=max_retries, logger=None
backoff.constant, Exception, max_tries=max_retries + 1, logger=None
)
def fetch_prompts():
return self.api.prompts.get(
Expand Down
9 changes: 9 additions & 0 deletions langfuse/_client/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,12 @@

**Default value**: ``True``
"""

LANGFUSE_TIMEOUT = "LANGFUSE_TIMEOUT"
"""
.. envvar: LANGFUSE_TIMEOUT

Controls the timeout for all API requests in seconds

**Default value**: ``5``
"""
2 changes: 1 addition & 1 deletion tests/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ def test_get_expired_prompt_when_failing_fetch(mock_time, langfuse: Langfuse):
break
sleep(0.1)

assert mock_server_call.call_count == 2
assert mock_server_call.call_count == 3
assert result_call_2 == prompt_client


Expand Down