feat(langchain): add ChatAnthropic support to langchain instrumentation#188
Conversation
Adds ChatAnthropic to the list of supported chat models in the LangChain callback handler. Previously only ChatOpenAI and ChatBedrock were instrumented; Anthropic users received no spans. Changes: - Add ChatAnthropic to the allowlist in on_chat_model_start - Add 'model' param key for model name extraction (Anthropic uses 'model', not 'model_name' or 'model_id') - Add 'stop_sequences' fallback for stop param (Anthropic's param name) - Add 'max_tokens' fallback for max tokens param - Add langchain-anthropic to test requirements - Add ChatAnthropic fixture and VCR cassette test Fixes open-telemetry#139
There was a problem hiding this comment.
Pull request overview
Adds ChatAnthropic coverage to the LangChain GenAI instrumentation so Anthropic chat invocations emit spans and model/request parameters are captured consistently with existing OpenAI/Bedrock handling.
Changes:
- Extend the
on_chat_model_startallowlist and parameter key extraction to supportChatAnthropic(model/stop/max_tokens fallbacks). - Add a
ChatAnthropictest fixture, dependency, and a VCR-backed test/cassette to validate span creation and model attribution.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-genai-langchain/src/opentelemetry/instrumentation/genai/langchain/callback_handler.py | Adds ChatAnthropic to supported chat models and introduces Anthropic-specific request param fallbacks. |
| instrumentation/opentelemetry-instrumentation-genai-langchain/tests/test_llm_call.py | Adds a VCR-backed test asserting Anthropic invocation produces a span and captures the request model. |
| instrumentation/opentelemetry-instrumentation-genai-langchain/tests/conftest.py | Adds a ChatAnthropic fixture for use in tests. |
| instrumentation/opentelemetry-instrumentation-genai-langchain/tests/requirements.txt | Adds langchain-anthropic to the test dependencies. |
| instrumentation/opentelemetry-instrumentation-genai-langchain/tests/cassettes/test_chat_anthropic_claude_sonnet_llm_call.yaml | Adds the recorded cassette for the new Anthropic test. |
| frequency_penalty = params.get("frequency_penalty") | ||
| presence_penalty = params.get("presence_penalty") | ||
| stop_sequences = params.get("stop") | ||
| stop_sequences = params.get("stop") or params.get("stop_sequences") |
| seed = params.get("seed") | ||
| temperature = params.get("temperature") | ||
| max_tokens = params.get("max_completion_tokens") | ||
| max_tokens = params.get("max_completion_tokens") or params.get("max_tokens") |
| spans = span_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span = spans[0] | ||
| assert span.attributes.get("gen_ai.request.model") == "claude-sonnet-4-5" |
|
This PR has review comments. Review suggestions, whether from maintainers or automated reviewers, aren't always correct or required. Please evaluate each comment on its merits, then make sure each thread has a clear outcome. For example, link to the commit if you applied a suggestion, explain why it wasn't applied, or ask a follow-up question. Automation flags a PR for human review once every review thread has a reply or is marked as resolved. Status across open PRs is visible on the pull request dashboard. |
|
question(non-blocking): Can you please check if we need to add tool support for anthropic? |
|
@eternalcuriouslearner Good question — the existing on_llm_end handler already captures tool calls via chat_generation.message.tool_calls, which reads from LangChain's unified message format and is provider-agnostic. So tool calls through ChatAnthropic should flow through that path without additional changes. That said, I haven't tested a tool-calling scenario with ChatAnthropic specifically — happy to add a test for it if you'd like that coverage before merging. |
Fixes #139
Summary
Adds
ChatAnthropicto the list of supported chat models in the LangChain callback handler. Previously onlyChatOpenAIandChatBedrockwere instrumented; Anthropic users received no spans at all.Changes
ChatAnthropicto the allowlist inon_chat_model_start"model"param key for model name extraction (Anthropic usesmodel, notmodel_nameormodel_id)"stop_sequences"fallback for stop param (Anthropic's native param name)"max_tokens"fallback for max tokens paramlangchain-anthropicto test requirementsChatAnthropicfixture and VCR cassette testNotes
The implementation approach follows the existing pattern for
ChatBedrock— provider-specific param key fallbacks inon_chat_model_start. The original fix was prototyped in opentelemetry-python-contrib#4496 by @salaboy before the repo migration; this ports and updates it to the current codebase structure.