LangfuseLangfuse Python SDK 4.15.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson

Errors and log levels

An exception inside a traced function marks its observation with level ERROR and the message, then reaches your code as usual. Set WARNING yourself for softer problems.

A support desk fails in two ways, and Langfuse records them differently. A refund the desk is not allowed to make, over 100 euros here, is an error. An order that does not exist is not an error, but it is worth finding later. Langfuse gives every observation a level, one of DEBUG, DEFAULT, WARNING and ERROR, and a status message to say why.

An exception

Examplelevels.py, after the setup lines with observe
@observe(name="refund-order", as_type="tool")
def refund_order(order_id, amount):
    if amount > 100:
        raise PermissionError(f"refund of {amount} euros needs approval")
    return "refunded"


for amount in [30, 250]:
    try:
        print(refund_order("A17", amount))
    except PermissionError as error:
        print("failed:", error)
Examplelevels.py, continued
langfuse.flush()
local_langfuse.tree("level", "status_message")
Example
python levels.py

The 250 euro refund raised PermissionError. The decorator recorded it on the observation, level ERROR and the exception's message as status message, and let it continue to your except. The 30 euro refund kept the default level, shown as None because nothing was set. An error message is span data, so a message containing a card number sends it to Langfuse.

A warning you set

Examplewarn.py, after the setup lines and ORDERS
@observe(name="lookup-order", as_type="tool")
def lookup_order(order_id):
    status = ORDERS.get(order_id, "not found")
    if status == "not found":
        langfuse.update_current_span(level="WARNING", status_message=f"no order {order_id}")
    return status


lookup_order("A17")
lookup_order("B22")
Example
python warn.py

update_current_span set a warning on the B22 lookup only. In Langfuse's interface, a trace's observations can be filtered by level, so a missing order stands out.

Try it yourself
  • Raise ValueError inside lookup_order for an empty order id and print the tree.
  • Catch the PermissionError inside refund_order and return a string. What level is recorded now?
  • Set level="DEBUG" on the A17 lookup.

Slow is fine. Stopping is the only problem.