Skip to content

Conversation

@NiclasvanEyk
Copy link

@NiclasvanEyk NiclasvanEyk commented Dec 1, 2025

This PR adds the ability to pass a callable to the fallback argument of the get_prompt function.
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 fallback argument, 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:

from utils import get_fallback_prompt_from_db

# Unnecessarily issues a DB query on every call
prompt = client.get_prompt("my_prompt", fallback=get_fallback_prompt_from_db("my_prompt"))

# Alternative: awkward try-catch and trying twice
prompt = None
try:
    prompt = client.get_prompt("my_prompt")
except Exception:
    prompt = client.get_prompt("my_prompt", fallback=get_fallback_prompt_from_db("my_prompt"))

After this MR:

from utils import get_fallback_prompt_from_db

# Only called when necessary, no awkward try-catch
prompt = client.get_prompt("my_prompt", fallback=lambda: get_fallback_prompt_from_db("my_prompt"))

Disclaimer: Experimental PR review

Greptile Overview

Greptile Summary

This PR extends the get_prompt fallback 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 using callable() before invoking the fallback.

Key changes:

  • Updated type signatures for all three get_prompt overloads to accept Callable[[], str] or Callable[[], List[ChatMessageDict]]
  • Added runtime check fallback() if callable(fallback) else fallback in langfuse/_client/client.py:3517
  • Added test coverage for callable text prompt fallback

The change is well-implemented with minimal risk. Test coverage could be enhanced with a callable chat prompt fallback test.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is straightforward and well-typed. It adds a simple runtime check using Python's built-in 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.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
langfuse/_client/client.py 5/5 Updated type hints to allow callable fallback arguments and added callable check in fallback prompt construction. Implementation is clean and safe.
tests/test_prompt.py 4/5 Added test for callable text prompt fallback. Test coverage is good but missing test for callable chat prompt fallback.

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)
Loading

@CLAassistant
Copy link

CLAassistant commented Dec 1, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@NiclasvanEyk NiclasvanEyk force-pushed the fallbacks-via-callables branch from 6ae2db3 to f492f72 Compare December 8, 2025 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants