Skip to content

Commit 6ae2db3

Browse files
committed
feat(client): allow fallback prompt to be supplied by a callable
1 parent bfbfac7 commit 6ae2db3

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

langfuse/_client/client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,7 +3407,10 @@ def get_prompt(
34073407
label: Optional[str] = None,
34083408
type: Literal["chat"],
34093409
cache_ttl_seconds: Optional[int] = None,
3410-
fallback: Optional[List[ChatMessageDict]] = None,
3410+
fallback: Union[
3411+
Optional[List[ChatMessageDict]],
3412+
Optional[Callable[[], List[ChatMessageDict]]],
3413+
] = None,
34113414
max_retries: Optional[int] = None,
34123415
fetch_timeout_seconds: Optional[int] = None,
34133416
) -> ChatPromptClient: ...
@@ -3421,7 +3424,7 @@ def get_prompt(
34213424
label: Optional[str] = None,
34223425
type: Literal["text"] = "text",
34233426
cache_ttl_seconds: Optional[int] = None,
3424-
fallback: Optional[str] = None,
3427+
fallback: Union[Optional[str], Optional[Callable[[], str]]] = None,
34253428
max_retries: Optional[int] = None,
34263429
fetch_timeout_seconds: Optional[int] = None,
34273430
) -> TextPromptClient: ...
@@ -3434,7 +3437,12 @@ def get_prompt(
34343437
label: Optional[str] = None,
34353438
type: Literal["chat", "text"] = "text",
34363439
cache_ttl_seconds: Optional[int] = None,
3437-
fallback: Union[Optional[List[ChatMessageDict]], Optional[str]] = None,
3440+
fallback: Union[
3441+
Optional[List[ChatMessageDict]],
3442+
Optional[str],
3443+
Optional[Callable[[], str]],
3444+
Optional[Callable[[], List[ChatMessageDict]]],
3445+
] = None,
34383446
max_retries: Optional[int] = None,
34393447
fetch_timeout_seconds: Optional[int] = None,
34403448
) -> PromptClient:
@@ -3454,7 +3462,7 @@ def get_prompt(
34543462
cache_ttl_seconds: Optional[int]: Time-to-live in seconds for caching the prompt. Must be specified as a
34553463
keyword argument. If not set, defaults to 60 seconds. Disables caching if set to 0.
34563464
type: Literal["chat", "text"]: The type of the prompt to retrieve. Defaults to "text".
3457-
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.
3465+
fallback: Union[Optional[List[ChatMessageDict]], Optional[str], Optional[Callable[[], str]], Optional[Callable[[], List[ChatMessageDict]]]]: 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.
34583466
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.
34593467
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.
34603468
@@ -3506,7 +3514,7 @@ def get_prompt(
35063514

35073515
fallback_client_args: Dict[str, Any] = {
35083516
"name": name,
3509-
"prompt": fallback,
3517+
"prompt": fallback() if callable(fallback) else fallback,
35103518
"type": type,
35113519
"version": version or 0,
35123520
"config": {},

tests/test_prompt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,25 @@ def test_fallback_text_prompt():
13031303
)
13041304

13051305

1306+
def test_fallback_text_prompt_via_callable():
1307+
langfuse = Langfuse()
1308+
fallback_text_prompt = "this is a fallback text prompt with {{variable}}"
1309+
1310+
def fallback_provider():
1311+
return fallback_text_prompt
1312+
1313+
# Should throw an error if prompt not found and no fallback provided
1314+
with pytest.raises(Exception):
1315+
langfuse.get_prompt("nonexistent_prompt")
1316+
1317+
prompt = langfuse.get_prompt("nonexistent_prompt", fallback=fallback_provider)
1318+
1319+
assert prompt.prompt == fallback_text_prompt
1320+
assert (
1321+
prompt.compile(variable="value") == "this is a fallback text prompt with value"
1322+
)
1323+
1324+
13061325
def test_fallback_chat_prompt():
13071326
langfuse = Langfuse()
13081327
fallback_chat_prompt = [

0 commit comments

Comments
 (0)