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
@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)langfuse.flush()
local_langfuse.tree("level", "status_message")python levels.pyThe 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
@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")python warn.pyupdate_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.
- Raise
ValueErrorinsidelookup_orderfor an empty order id and print the tree. - Catch the
PermissionErrorinsiderefund_orderand return a string. What level is recorded now? - Set
level="DEBUG"on the A17 lookup.
Slow is fine. Stopping is the only problem.