Skip to content

Commit ede6454

Browse files
authored
fix: set timeout to 5 seconds (#1211)
1 parent 4c82a4b commit ede6454

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

langfuse/_client/client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
LANGFUSE_PUBLIC_KEY,
3131
LANGFUSE_SAMPLE_RATE,
3232
LANGFUSE_SECRET_KEY,
33+
LANGFUSE_TIMEOUT,
3334
LANGFUSE_TRACING_ENABLED,
3435
LANGFUSE_TRACING_ENVIRONMENT,
3536
)
@@ -90,7 +91,7 @@ class Langfuse:
9091
public_key (Optional[str]): Your Langfuse public API key. Can also be set via LANGFUSE_PUBLIC_KEY environment variable.
9192
secret_key (Optional[str]): Your Langfuse secret API key. Can also be set via LANGFUSE_SECRET_KEY environment variable.
9293
host (Optional[str]): The Langfuse API host URL. Defaults to "https://cloud.langfuse.com". Can also be set via LANGFUSE_HOST environment variable.
93-
timeout (Optional[int]): Timeout in seconds for API requests. Defaults to 30 seconds.
94+
timeout (Optional[int]): Timeout in seconds for API requests. Defaults to 5 seconds.
9495
httpx_client (Optional[httpx.Client]): Custom httpx client for making non-tracing HTTP requests. If not provided, a default client will be created.
9596
debug (bool): Enable debug logging. Defaults to False. Can also be set via LANGFUSE_DEBUG environment variable.
9697
tracing_enabled (Optional[bool]): Enable or disable tracing. Defaults to True. Can also be set via LANGFUSE_TRACING_ENABLED environment variable.
@@ -168,6 +169,8 @@ def __init__(
168169
f"Sample rate must be between 0.0 and 1.0, got {sample_rate}"
169170
)
170171

172+
timeout = timeout or int(os.environ.get(LANGFUSE_TIMEOUT, 5))
173+
171174
self._tracing_enabled = (
172175
tracing_enabled
173176
and os.environ.get(LANGFUSE_TRACING_ENABLED, "True") != "False"
@@ -1840,7 +1843,7 @@ def resolve_media_references(
18401843
obj: Any,
18411844
resolve_with: Literal["base64_data_uri"],
18421845
max_depth: int = 10,
1843-
content_fetch_timeout_seconds: int = 10,
1846+
content_fetch_timeout_seconds: int = 5,
18441847
):
18451848
"""Replace media reference strings in an object with base64 data URIs.
18461849
@@ -1857,7 +1860,7 @@ def resolve_media_references(
18571860
resolve_with: The representation of the media content to replace the media reference string with.
18581861
Currently only "base64_data_uri" is supported.
18591862
max_depth: int: The maximum depth to traverse the object. Default is 10.
1860-
content_fetch_timeout_seconds: int: The timeout in seconds for fetching media content. Default is 10.
1863+
content_fetch_timeout_seconds: int: The timeout in seconds for fetching media content. Default is 5.
18611864
18621865
Returns:
18631866
A deep copy of the input object with all media references replaced with base64 data URIs where possible.
@@ -1947,7 +1950,7 @@ def get_prompt(
19471950
type: Literal["chat", "text"]: The type of the prompt to retrieve. Defaults to "text".
19481951
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.
19491952
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.
1950-
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.
1953+
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.
19511954
19521955
Returns:
19531956
The prompt object retrieved from the cache or directly fetched if not cached or expired of type
@@ -2066,7 +2069,7 @@ def _fetch_prompt_and_update_cache(
20662069
try:
20672070

20682071
@backoff.on_exception(
2069-
backoff.constant, Exception, max_tries=max_retries, logger=None
2072+
backoff.constant, Exception, max_tries=max_retries + 1, logger=None
20702073
)
20712074
def fetch_prompts():
20722075
return self.api.prompts.get(

langfuse/_client/environment_variables.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,12 @@
119119
120120
**Default value**: ``True``
121121
"""
122+
123+
LANGFUSE_TIMEOUT = "LANGFUSE_TIMEOUT"
124+
"""
125+
.. envvar: LANGFUSE_TIMEOUT
126+
127+
Controls the timeout for all API requests in seconds
128+
129+
**Default value**: ``5``
130+
"""

tests/test_prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ def test_get_expired_prompt_when_failing_fetch(mock_time, langfuse: Langfuse):
863863
break
864864
sleep(0.1)
865865

866-
assert mock_server_call.call_count == 2
866+
assert mock_server_call.call_count == 3
867867
assert result_call_2 == prompt_client
868868

869869

0 commit comments

Comments
 (0)