feat(client): allow fallback prompt to be supplied by a callable #1460
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the ability to pass a callable to the
fallbackargument of theget_promptfunction.It makes it easier to e.g. fetch a fallback prompt from an external location, such as a database or a file.
E.g. since the recent Cloudflare outage we store fallback prompts as JSON files in our deployed artifacts.
Right now we have the choice of either always loading them to pass them as the
fallbackargument, or introduce our own try-catch logic and only load them once an error occurs. A better/more ergonomic solution would be to pass a function that will load the fallback prompt in case of a failure, which is exactly what this PR enables.Examples
Before this MR:
After this MR:
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
This PR extends the
get_promptfallback functionality to accept callable functions that return prompt values, allowing lazy evaluation of fallback prompts. The implementation cleanly adds callable support by updating type hints and adding a runtime check usingcallable()before invoking the fallback.Key changes:
get_promptoverloads to acceptCallable[[], str]orCallable[[], List[ChatMessageDict]]fallback() if callable(fallback) else fallbackin langfuse/_client/client.py:3517The change is well-implemented with minimal risk. Test coverage could be enhanced with a callable chat prompt fallback test.
Confidence Score: 5/5
callable()function to support lazy evaluation of fallback prompts. The change is backward compatible (static fallbacks still work), properly typed with Union types across all overloads, and includes test coverage for the new functionality. No security concerns or edge cases that would cause runtime errors.Important Files Changed
File Analysis
Sequence Diagram
sequenceDiagram participant User participant Langfuse participant PromptCache participant FallbackCallable participant PromptClient User->>Langfuse: get_prompt(name, fallback=callable_fn) Langfuse->>PromptCache: get(cache_key) PromptCache-->>Langfuse: None (not cached) Langfuse->>Langfuse: _fetch_prompt_and_update_cache() Note over Langfuse: Fetch fails (Exception) Langfuse->>Langfuse: Check if fallback exists Langfuse->>Langfuse: callable(fallback)? alt Fallback is callable Langfuse->>FallbackCallable: fallback() FallbackCallable-->>Langfuse: prompt value else Fallback is static Note over Langfuse: Use fallback directly end Langfuse->>PromptClient: Create client with fallback prompt PromptClient-->>User: Return prompt client (is_fallback=True)