Skip to content

chore(closes OPEN-10731): async LangChain callback handler tool call …#644

Merged
viniciusdsmello merged 1 commit into
mainfrom
gustavo/open-10731-async-langchain-callback-handler-tool-call-improvements
May 14, 2026
Merged

chore(closes OPEN-10731): async LangChain callback handler tool call …#644
viniciusdsmello merged 1 commit into
mainfrom
gustavo/open-10731-async-langchain-callback-handler-tool-call-improvements

Conversation

@gustavocidornelas
Copy link
Copy Markdown
Contributor

Pull Request

Summary

Fix the LangChain callback handler so ToolMessage.artifact (returned by @tool(response_format="content_and_artifact")) is captured on the trace instead of being silently dropped. When the artifact is a list of LangChain Document instances, the page-content strings are also promoted to the trace-level
context reserved column, matching the existing retriever behavior.

Changes

  • Add _extract_tool_output helper that handles three shapes LangChain may pass to on_tool_end: ToolMessage
    with .content/.artifact, raw (content, artifact) tuple, and plain string (backward-compat).
  • Add _is_document_list predicate (non-empty sequence whose elements all have page_content).
  • Rewrite _handle_tool_end to store the artifact in step.metadata["artifact"] and, when it's a document list,
    populate trace-level context. Step output stays a string so downstream agent loops feeding step.output back to
    the LLM aren't disturbed.
  • Add a run_id → trace map on OpenlayerHandlerMixin (maintained in _start_step/_end_step on both sync and
    async handlers) and a _find_trace helper so context promotion works in pure-callback mode (no outer tracer.trace()
    wrapper).
  • Wire the new _find_trace lookup into _handle_retriever_end and the _handle_chain_end source_documents branch so those paths also populate context in pure-callback mode.
  • New test file tests/test_langchain_callback.py with 6 cases (plain-string, tuple, ToolMessage with dict
    artifact, document-list promotion sync + async, end-to-end metadata serialization).
  • Example script examples/tracing/langchain/tool_artifact_example.py reproducing the reporter's scenario.

Context

OPEN-10731: Async LangChain callback handler tool call improvements

Testing

  • Manual testing

Monitoring

  • No expected impact

@viniciusdsmello viniciusdsmello merged commit b6b9ba3 into main May 14, 2026
5 checks passed
@viniciusdsmello viniciusdsmello deleted the gustavo/open-10731-async-langchain-callback-handler-tool-call-improvements branch May 14, 2026 17:08
@stainless-app stainless-app Bot mentioned this pull request May 14, 2026
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