Skip to content

fix(api): recover LangChain tool calls dropped during openinference ingestion#4407

Open
mmabrouk wants to merge 1 commit into
fix/openinference-tool-parsingfrom
fix/langchain-tool-call-ingestion
Open

fix(api): recover LangChain tool calls dropped during openinference ingestion#4407
mmabrouk wants to merge 1 commit into
fix/openinference-tool-parsingfrom
fix/langchain-tool-call-ingestion

Conversation

@mmabrouk
Copy link
Copy Markdown
Member

Stacked on top of #4397 (fix/openinference-tool-parsing). Review and merge that one first.

Summary

A chat conversation traced from LangChain (via openinference) lost its tool-call structure during ingestion. The stored ag.data.inputs.prompt showed an assistant message with no tool_calls and a tool message with no tool_call_id. Replaying such a conversation failed with Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.

Root cause: the openinference LangChain instrumentation flattens llm.input_messages with only role and content. The assistant tool_calls and the tool tool_call_id/name survive only inside input.value, serialized in the LangChain constructor format ({"messages": [[{lc, type, id, kwargs}, ...]]}). For chat spans the adapter then deletes the generic input.value mapping in favor of the flattened messages, making the loss permanent.

The fix teaches OpenInferenceAdapter to recover the dropped fields. It parses input.value, recognizes the LangChain serialized shape, and merges the recovered tool_calls (converted from LangChain {id, name, args, type} to the OpenAI {id, type: "function", function: {name, arguments}} shape), tool_call_id, and name onto the prompt by index. The merge uses setdefault, so it only fills fields the flattened messages lacked, and it is gated to genuine LangChain payloads (the langchain_core / constructor markers), so other integrations are untouched.

Testing

Verified locally

Restarted the API and tracing worker, re-ran the n8n LangChain agent, and confirmed the newly ingested trace now carries tool_calls on the assistant and tool_call_id/name on the tool message. Combined with the playground loader fix (#4406), the trace then replays without the OpenAI tool-pairing error.

Added or updated tests

Four unit tests in test_openinference_adapter.py, built from a real n8n HTTP_Request span: tool_calls recovered onto the assistant in OpenAI shape, the tool message link (tool_call_id + name) recovered, role and content preserved, and a non-LangChain input.value left untouched. Full adapter suite passes (16 tests). ruff format and ruff check clean.

QA follow-up

The recovery runs at ingestion, so it only affects newly ingested traces, not ones stored before this change. Validate with fresh traces from LangChain agents that use tools, including multi-tool turns.

Demo

N/A. Backend ingestion change.

Checklist

  • I have included a video or screen recording for UI changes, or marked Demo as N/A
  • Relevant tests pass locally
  • Relevant linting and formatting pass locally
  • I have signed the CLA, or I will sign it when the bot prompts me

@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Ready Ready Preview, Comment May 24, 2026 3:24pm

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1f800c35-0a3f-4bb4-96c8-7dfba7e18866

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/langchain-tool-call-ingestion

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels May 22, 2026
@mmabrouk mmabrouk requested review from jp-agenta and junaway May 24, 2026 12:49
…ngestion

The openinference LangChain instrumentation flattens llm.input_messages with
only role and content. The assistant tool_calls and the tool tool_call_id/name
survive only inside input.value, serialized in the LangChain constructor format.
For chat spans the adapter then deletes the input.value mapping in favor of the
flattened messages, so the tool fields were lost. A conversation replayed from
such a trace failed OpenAI validation, since a tool message had no preceding
assistant declaring its call.

The adapter now parses input.value, recognizes the LangChain serialized shape,
and merges the recovered tool_calls (converted to the OpenAI shape),
tool_call_id, and name onto the prompt by index. The merge uses setdefault so it
only fills fields the flattened messages lacked, and it is gated to genuine
LangChain payloads so other integrations are untouched.

Adds four unit tests built from a real n8n HTTP_Request span.
@mmabrouk mmabrouk force-pushed the fix/openinference-tool-parsing branch from b512ea4 to 1ad49e4 Compare May 24, 2026 15:23
@mmabrouk mmabrouk force-pushed the fix/langchain-tool-call-ingestion branch from cfc15be to 451c2be Compare May 24, 2026 15:23
@github-actions
Copy link
Copy Markdown
Contributor

Railway Preview Environment

Preview URL https://gateway-production-e9d2.up.railway.app/w
Project agenta-oss-pr-4407
Image tag pr-4407-c8a25f6
Status Deployed
Railway logs Open logs
Workflow logs View workflow run
Updated at 2026-05-24T15:33:01.702Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant