Skip to content

Commit f492f72

Browse files
committed
feat(client): allow fallback prompt to be supplied by a callable
1 parent 42d7e56 commit f492f72

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
@@ -3411,7 +3411,10 @@ def get_prompt(
34113411
label: Optional[str] = None,
34123412
type: Literal["chat"],
34133413
cache_ttl_seconds: Optional[int] = None,
3414-
fallback: Optional[List[ChatMessageDict]] = None,
3414+
fallback: Union[
3415+
Optional[List[ChatMessageDict]],
3416+
Optional[Callable[[], List[ChatMessageDict]]],
3417+
] = None,
34153418
max_retries: Optional[int] = None,
34163419
fetch_timeout_seconds: Optional[int] = None,
34173420
) -> ChatPromptClient: ...
@@ -3425,7 +3428,7 @@ def get_prompt(
34253428
label: Optional[str] = None,
34263429
type: Literal["text"] = "text",
34273430
cache_ttl_seconds: Optional[int] = None,
3428-
fallback: Optional[str] = None,
3431+
fallback: Union[Optional[str], Optional[Callable[[], str]]] = None,
34293432
max_retries: Optional[int] = None,
34303433
fetch_timeout_seconds: Optional[int] = None,
34313434
) -> TextPromptClient: ...
@@ -3438,7 +3441,12 @@ def get_prompt(
34383441
label: Optional[str] = None,
34393442
type: Literal["chat", "text"] = "text",
34403443
cache_ttl_seconds: Optional[int] = None,
3441-
fallback: Union[Optional[List[ChatMessageDict]], Optional[str]] = None,
3444+
fallback: Union[
3445+
Optional[List[ChatMessageDict]],
3446+
Optional[str],
3447+
Optional[Callable[[], str]],
3448+
Optional[Callable[[], List[ChatMessageDict]]],
3449+
] = None,
34423450
max_retries: Optional[int] = None,
34433451
fetch_timeout_seconds: Optional[int] = None,
34443452
) -> PromptClient:
@@ -3458,7 +3466,7 @@ def get_prompt(
34583466
cache_ttl_seconds: Optional[int]: Time-to-live in seconds for caching the prompt. Must be specified as a
34593467
keyword argument. If not set, defaults to 60 seconds. Disables caching if set to 0.
34603468
type: Literal["chat", "text"]: The type of the prompt to retrieve. Defaults to "text".
3461-
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.
3469+
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.
34623470
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.
34633471
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.
34643472
@@ -3510,7 +3518,7 @@ def get_prompt(
35103518

35113519
fallback_client_args: Dict[str, Any] = {
35123520
"name": name,
3513-
"prompt": fallback,
3521+
"prompt": fallback() if callable(fallback) else fallback,
35143522
"type": type,
35153523
"version": version or 0,
35163524
"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)