fix(langchain): add provider metadata, route tools to metadata, allowlist span fields#593
Merged
Merged
Conversation
…list span fields Align the LangChain callback handler with `.agents/skills/sdk-integrations/SKILL.md`. - **Missing `metadata.provider`** — spec requires every `llm` span to carry both `metadata.model` and `metadata.provider`. Derive provider at start from `serialized["id"]` (strip `langchain_` prefix + small alias table for google/aws/azure/mistral) and at end from `response_metadata.model_provider` as a fallback. - **Tools in `invocation_params` instead of `metadata.tools`** — lift `tools` / `functions` to `metadata.tools` and `tool_choice` / `function_call` to `metadata.tool_choice` via `_split_tools`. - **Denylist → allowlist** — dropped the `exclude_metadata_props` regex (dead code — was set but never applied) and stopped spreading `**kwargs` and the full `serialized` runnable dump into span metadata across every callback. Each callback now captures only the LangChain user metadata (preserved as `metadata.metadata` so `deepagents` tests still resolve `ls_integration` / `lc_agent_name` / `lc_versions`), tags, and known-safe fields. - **Excess serialization** — `on_tool_start` no longer duplicates the tool payload as `inputs` + `input_str` + `input` (parsed) + `serialized`; keeps only the parsed input. Removed the raw kwargs dump in every error callback. - **`on_agent_action` span type** — `LLM` → `TOOL` (the callback fires when the agent picks a tool, not when it calls an LLM). - Trimmed narrator comments per the skill guidance. No new mocks, no re-recorded cassettes, no new test files. Existing VCR suite (`test_callbacks.py`, `test_anthropic.py`, `test_deepagents.py`) covers every changed path; added positive `metadata.provider` / `metadata.tools` assertions on the tests whose cassettes exercise them. The LangChain-native message / `LLMResult` shape in `input` / `output` is left as-is — the Braintrust backend already normalizes it. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Abhijeet Prasad (AbhiPrasad)
approved these changes
Jul 17, 2026
Abhijeet Prasad (AbhiPrasad)
enabled auto-merge (squash)
July 17, 2026 20:37
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Align the LangChain callback handler with
.agents/skills/sdk-integrations/SKILL.md. Audit turned up five issues:metadata.provideron every span — spec requires everyllmspan to carry bothmetadata.modelandmetadata.provider. Derive provider at start fromserialized["id"](striplangchain_prefix + small alias table for google/aws/azure/mistral) and at end fromresponse_metadata.model_provideras a fallback.invocation_paramsinstead ofmetadata.tools— spec says tool definitions live inmetadata.tools. Lifttools/functionstometadata.toolsandtool_choice/function_calltometadata.tool_choicevia_split_tools, stripping them from the invocation params that flow into metadata.exclude_metadata_propsregex (dead code — was set but never applied) and stopped spreading**kwargsand the fullserializedrunnable dump into span metadata across every callback. Each callback now captures only LangChain user metadata (preserved asmetadata.metadatasodeepagentstests still resolvels_integration/lc_agent_name/lc_versions), tags, and known-safe fields.on_tool_startno longer duplicates the tool payload asinputs+input_str+input(parsed) +serialized; keeps only the parsed input. Removed the raw kwargs dump in every error callback.on_agent_actionspan type —LLM→TOOL(the callback fires when the agent picks a tool, not when it calls an LLM).Also trimmed narrator comments per the skill's "default to no comments" guidance, and merged the two independent
_walk_generationspasses inon_llm_endinto a single_model_and_provider_from_responsewalk.The LangChain-native message /
LLMResultshape ininput/outputis left as-is — the Braintrust backend already normalizes it.Test plan
metadata.provider("openai"forChatOpenAIspans,"anthropic"forChatAnthropic), andmetadata.toolspresence for thetest_tool_usagecase.cd py && CI=1 BRAINTRUST_TEST_PACKAGE_VERSION=latest pytest src/braintrust/integrations/langchain/— 18 passed, 1 skipped.cd py && CI=1 BRAINTRUST_TEST_PACKAGE_VERSION=0.3.28 pytest src/braintrust/integrations/langchain/test_callbacks.py src/braintrust/integrations/langchain/test_anthropic.py— 13 passed, 1 skipped.pylint --errors-onlyclean.🤖 Generated with Claude Code
Created by abhijeet
Slack thread