|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from langchain.messages import HumanMessage |
8 | | -from langchain_core.messages import AIMessage |
| 8 | +from langchain_core.messages import AIMessage, ToolMessage |
9 | 9 | from langchain_core.output_parsers import StrOutputParser |
10 | 10 | from langchain_core.outputs import ChatGeneration, ChatResult, Generation, LLMResult |
11 | 11 | from langchain_core.prompts import ChatPromptTemplate |
@@ -791,6 +791,39 @@ class DummyControlFlowError(RuntimeError): |
791 | 791 | assert not _has_run_state(handler, retriever_run_id) |
792 | 792 |
|
793 | 793 |
|
| 794 | +def test_handled_tool_error_marks_observation_error( |
| 795 | + langfuse_memory_client, get_span, json_attr |
| 796 | +): |
| 797 | + handler = CallbackHandler() |
| 798 | + run_id = uuid4() |
| 799 | + |
| 800 | + handler.on_tool_start( |
| 801 | + {"name": "failing_tool"}, |
| 802 | + '{"query": "x"}', |
| 803 | + run_id=run_id, |
| 804 | + ) |
| 805 | + handler.on_tool_end( |
| 806 | + ToolMessage( |
| 807 | + content="handled failure", |
| 808 | + tool_call_id="call_1", |
| 809 | + status="error", |
| 810 | + ), |
| 811 | + run_id=run_id, |
| 812 | + ) |
| 813 | + |
| 814 | + langfuse_memory_client.flush() |
| 815 | + span = get_span("failing_tool") |
| 816 | + |
| 817 | + assert span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "ERROR" |
| 818 | + assert ( |
| 819 | + span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE] |
| 820 | + == "handled failure" |
| 821 | + ) |
| 822 | + assert json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT)["status"] == ( |
| 823 | + "error" |
| 824 | + ) |
| 825 | + |
| 826 | + |
794 | 827 | def test_pending_resume_contexts_are_capped(langfuse_memory_client, monkeypatch): |
795 | 828 | class DummyControlFlowError(RuntimeError): |
796 | 829 | pass |
|
0 commit comments