Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions langfuse/langchain/CallbackHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,22 +978,25 @@ def on_tool_start(
observation_type = self._get_observation_type_from_serialized(
serialized, "tool", **kwargs
)
tool_input = kwargs.get("inputs")
if tool_input is None:
tool_input = input_str

parent_observation = self._get_parent_observation(parent_run_id)
if isinstance(parent_observation, Langfuse):
span = parent_observation.start_observation(
trace_context=self._trace_context,
name=self.get_langchain_run_name(serialized, **kwargs),
as_type=observation_type,
input=input_str,
input=tool_input,
metadata=meta,
level="DEBUG" if tags and LANGSMITH_TAG_HIDDEN in tags else None,
)
else:
span = parent_observation.start_observation(
name=self.get_langchain_run_name(serialized, **kwargs),
as_type=observation_type,
input=input_str,
input=tool_input,
metadata=meta,
level="DEBUG" if tags and LANGSMITH_TAG_HIDDEN in tags else None,
)
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,33 @@ class DummyControlFlowError(RuntimeError):
assert not _has_run_state(handler, retriever_run_id)


def test_tool_start_prefers_structured_inputs_when_available(
langfuse_memory_client, get_span, json_attr
):
handler = CallbackHandler()
run_id = uuid4()
structured_inputs = {
"path": "/tmp/example.md",
"content": '# Title\n\nThis has "quotes" and JSON-like text: {"a": 1}',
}

handler.on_tool_start(
{"name": "write_document"},
str(structured_inputs),
run_id=run_id,
inputs=structured_inputs,
)
handler.on_tool_end("ok", run_id=run_id)

langfuse_memory_client.flush()
span = get_span("write_document")

assert (
json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_INPUT)
== structured_inputs
)


def test_handled_tool_error_marks_observation_error(
langfuse_memory_client, get_span, json_attr
):
Expand Down