diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index 2b2889545..67880ebc4 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -3919,7 +3919,7 @@ def get_prompt( fetch_timeout_seconds=fetch_timeout_seconds, ) except Exception as e: - if fallback: + if fallback is not None: langfuse_logger.warning( f"Returning fallback prompt for '{cache_key}' due to fetch error: {e}" ) diff --git a/tests/unit/test_prompt.py b/tests/unit/test_prompt.py index eadfb8221..7dcab0a61 100644 --- a/tests/unit/test_prompt.py +++ b/tests/unit/test_prompt.py @@ -222,6 +222,19 @@ def test_throw_when_failing_fetch_and_no_cache(langfuse): assert "Prompt not found" in str(exc_info.value) +def test_returns_empty_text_fallback_when_fetch_fails(langfuse): + prompt_name = "test_returns_empty_text_fallback_when_fetch_fails" + + mock_server_call = langfuse.api.prompts.get + mock_server_call.side_effect = Exception("Prompt not found") + + result = langfuse.get_prompt(prompt_name, fallback="", max_retries=0) + + assert result.is_fallback + assert result.prompt == "" + mock_server_call.assert_called_once() + + def test_using_custom_prompt_timeouts(langfuse): prompt_name = "test_using_custom_prompt_timeouts" prompt = Prompt_Text(