From cdd92765520ca1307005486439edd5ea071c7b9b Mon Sep 17 00:00:00 2001 From: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> Date: Wed, 11 Jun 2025 11:12:11 +0200 Subject: [PATCH 1/2] fix: set timeout to 5 seconds --- langfuse/_client/client.py | 13 ++++++++----- langfuse/_client/environment_variables.py | 9 +++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index adc543639..2837dcfef 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -30,6 +30,7 @@ LANGFUSE_PUBLIC_KEY, LANGFUSE_SAMPLE_RATE, LANGFUSE_SECRET_KEY, + LANGFUSE_TIMEOUT, LANGFUSE_TRACING_ENABLED, LANGFUSE_TRACING_ENVIRONMENT, ) @@ -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. @@ -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" @@ -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. @@ -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. @@ -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 @@ -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( diff --git a/langfuse/_client/environment_variables.py b/langfuse/_client/environment_variables.py index d9df459a0..7667ba213 100644 --- a/langfuse/_client/environment_variables.py +++ b/langfuse/_client/environment_variables.py @@ -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`` +""" From a49fc9616bd1c6680e606adef605ad0a560b8a7b Mon Sep 17 00:00:00 2001 From: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> Date: Wed, 11 Jun 2025 11:30:32 +0200 Subject: [PATCH 2/2] fix --- tests/test_prompt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_prompt.py b/tests/test_prompt.py index a712a8cc1..5630bdd41 100644 --- a/tests/test_prompt.py +++ b/tests/test_prompt.py @@ -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