From 0ae889a4056bd5926f54ca8681f7c17b03767380 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Fri, 13 Mar 2026 15:50:58 +0000 Subject: [PATCH] fix(langchain): Wrap finish_reason in array for gen_ai span attribute The gen_ai.response.finish_reasons attribute should be an array of strings per the semantic convention. Wrap the single finish_reason value in a list before setting the span data. Co-Authored-By: Claude Opus 4.6 --- sentry_sdk/integrations/langchain.py | 3 ++- tests/integrations/langchain/test_langchain.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/langchain.py b/sentry_sdk/integrations/langchain.py index 7a5e863e4d..d19d9bbdd5 100644 --- a/sentry_sdk/integrations/langchain.py +++ b/sentry_sdk/integrations/langchain.py @@ -554,7 +554,8 @@ def on_llm_end( finish_reason = generation.generation_info.get("finish_reason") if finish_reason is not None: span.set_data( - SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, finish_reason + SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, + [finish_reason], ) except AttributeError: pass diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index 8a8d646113..132da0a9a0 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -297,6 +297,12 @@ def test_langchain_agent( f"and include_prompts={include_prompts}" ) + # Verify finish_reasons is always an array of strings + assert chat_spans[0]["data"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "function_call" + ] + assert chat_spans[1]["data"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["stop"] + # Verify that available tools are always recorded regardless of PII settings for chat_span in chat_spans: span_data = chat_span.get("data", {})