The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
(Fix) langfuse - setting `LANGFUSE_FLUSH_INTERVAL` (#8007)
Ishaan Jaffdcc3bbc26430
diff --git a/litellm/integrations/langfuse/langfuse.py b/litellm/integrations/langfuse/langfuse.pyindex 20d2befe65a..125bf4e6866 100644--- a/litellm/integrations/langfuse/langfuse.py+++ b/litellm/integrations/langfuse/langfuse.py@@ -54,8 +54,8 @@ class LangFuseLogger:self.langfuse_host = "http://" + self.langfuse_hostself.langfuse_release = os.getenv("LANGFUSE_RELEASE")self.langfuse_debug = os.getenv("LANGFUSE_DEBUG")- self.langfuse_flush_interval = (- os.getenv("LANGFUSE_FLUSH_INTERVAL") or flush_interval+ self.langfuse_flush_interval = LangFuseLogger._get_langfuse_flush_interval(+ flush_interval)http_client = _get_httpx_client()self.langfuse_client = http_client.client@@ -708,6 +708,22 @@ class LangFuseLogger:"""Check if current langfuse version supports completion start time"""return Version(self.langfuse_sdk_version) >= Version("2.7.3")+ @staticmethod+ def _get_langfuse_flush_interval(flush_interval: int) -> int:+ """+ Get the langfuse flush interval to initialize the Langfuse client++ Reads `LANGFUSE_FLUSH_INTERVAL` from the environment variable.+ If not set, uses the flush interval passed in as an argument.++ Args:+ flush_interval: The flush interval to use if LANGFUSE_FLUSH_INTERVAL is not set++ Returns:+ [int] The flush interval to use to initialize the Langfuse client+ """+ return int(os.getenv("LANGFUSE_FLUSH_INTERVAL") or flush_interval)+def _add_prompt_to_generation_params(generation_params: dict,diff --git a/litellm/integrations/langfuse/langfuse_prompt_management.py b/litellm/integrations/langfuse/langfuse_prompt_management.pyindex 44419896c48..faa4a63491a 100644--- a/litellm/integrations/langfuse/langfuse_prompt_management.py+++ b/litellm/integrations/langfuse/langfuse_prompt_management.py@@ -81,7 +81,6 @@ def langfuse_client_init(langfuse_release = os.getenv("LANGFUSE_RELEASE")langfuse_debug = os.getenv("LANGFUSE_DEBUG")- langfuse_flush_interval = os.getenv("LANGFUSE_FLUSH_INTERVAL") or flush_intervalparameters = {"public_key": public_key,@@ -89,7 +88,9 @@ def langfuse_client_init("host": langfuse_host,"release": langfuse_release,"debug": langfuse_debug,- "flush_interval": langfuse_flush_interval, # flush interval in seconds+ "flush_interval": LangFuseLogger._get_langfuse_flush_interval(+ flush_interval+ ), # flush interval in seconds}if Version(langfuse.version.__version__) >= Version("2.6.0"):diff --git a/tests/logging_callback_tests/test_langfuse_unit_tests.py b/tests/logging_callback_tests/test_langfuse_unit_tests.pyindex 8f0f2637c69..e9c255c1a30 100644--- a/tests/logging_callback_tests/test_langfuse_unit_tests.py+++ b/tests/logging_callback_tests/test_langfuse_unit_tests.py@@ -294,6 +294,28 @@ def test_get_langfuse_tags():assert result == []+[email protected](os.environ, {}, clear=True) # Start with empty environment+def test_get_langfuse_flush_interval():+ """+ Test that _get_langfuse_flush_interval correctly reads from environment variable+ or falls back to the provided flush_interval+ """+ default_interval = 60++ # Test when env var is not set+ result = LangFuseLogger._get_langfuse_flush_interval(+ flush_interval=default_interval+ )+ assert result == default_interval++ # Test when env var is set+ with patch.dict(os.environ, {"LANGFUSE_FLUSH_INTERVAL": "120"}):+ result = LangFuseLogger._get_langfuse_flush_interval(+ flush_interval=default_interval+ )+ assert result == 120+def test_langfuse_e2e_sync(monkeypatch):from litellm import completionimport litellm@@ -320,3 +342,4 @@ def test_langfuse_e2e_sync(monkeypatch):time.sleep(3)assert langfuse_mock.called+