Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d4c4cd4
fix(langchain): Set agent name as gen_ai.agent.name
alexander-alderman-webb Mar 25, 2026
181d5cb
merge master
alexander-alderman-webb Mar 31, 2026
8cf3f81
typing
alexander-alderman-webb Mar 31, 2026
56ec48f
fix span description
alexander-alderman-webb Mar 31, 2026
d8c06f8
defensive check
alexander-alderman-webb Mar 31, 2026
36ca817
no agent name in stream
alexander-alderman-webb Mar 31, 2026
87ed060
feat(langchain): Record run_name in on_chat_model_start
alexander-alderman-webb Mar 31, 2026
ea94bfc
.
alexander-alderman-webb Mar 31, 2026
cd08d96
.
alexander-alderman-webb Mar 31, 2026
0d43616
simplify
alexander-alderman-webb Mar 31, 2026
568e6f7
truthy check
alexander-alderman-webb Mar 31, 2026
b9387b8
set run name
alexander-alderman-webb Apr 1, 2026
ed3e824
Merge branch 'webb/langchain/agent-name' into webb/langchain/pipeline…
alexander-alderman-webb Apr 1, 2026
cf70d07
Merge branch 'master' into webb/langchain/agent-name
alexander-alderman-webb Apr 1, 2026
1b6ddfa
Merge branch 'webb/langchain/agent-name' into webb/langchain/pipeline…
alexander-alderman-webb Apr 1, 2026
52eb5c3
more descriptive test name
alexander-alderman-webb Apr 1, 2026
637ee9c
Merge branch 'master' into webb/langchain/agent-name
alexander-alderman-webb Apr 14, 2026
412af15
merge and function_id
alexander-alderman-webb Apr 14, 2026
5bddf72
import order
alexander-alderman-webb Apr 14, 2026
1efa748
remove duplicate imports
alexander-alderman-webb Apr 14, 2026
a566ced
update assertion
alexander-alderman-webb Apr 14, 2026
fb388a9
update kwarg
alexander-alderman-webb Apr 14, 2026
efc9460
make openai test values consistent with previous values
alexander-alderman-webb Apr 14, 2026
1de30a1
update fixture arguments
alexander-alderman-webb Apr 14, 2026
42dbc3a
.
alexander-alderman-webb Apr 14, 2026
2c5f7d9
merge master
alexander-alderman-webb Apr 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ class SPANDATA:
Example: "Hello!"
"""

GEN_AI_FUNCTION_ID = "gen_ai.function_id"
"""
Framework-specific tracing label for the execution of a function or other unit of execution in a generative AI system.
Example: "my-awesome-function"
"""

GEN_AI_OPERATION_NAME = "gen_ai.operation.name"
"""
The name of the operation being performed.
Expand Down
7 changes: 7 additions & 0 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ def on_chat_model_start(
SPANDATA.GEN_AI_AGENT_NAME, agent_metadata["lc_agent_name"]
)

run_name = kwargs.get("name")
if run_name:
span.set_data(
SPANDATA.GEN_AI_FUNCTION_ID,
run_name,
Comment thread
alexander-alderman-webb marked this conversation as resolved.
)
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Comment thread
alexander-alderman-webb marked this conversation as resolved.

for key, attribute in DATA_FIELDS.items():
if key in all_params and all_params[key] is not None:
set_data_normalized(span, attribute, all_params[key], unpack=False)
Expand Down
45 changes: 25 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,26 +1265,31 @@ def streaming_chat_completions_model_response():

@pytest.fixture
def nonstreaming_chat_completions_model_response():
return openai.types.chat.ChatCompletion(
id="chatcmpl-test",
choices=[
openai.types.chat.chat_completion.Choice(
index=0,
finish_reason="stop",
message=openai.types.chat.ChatCompletionMessage(
role="assistant", content="Test response"
),
)
],
created=1234567890,
model="gpt-3.5-turbo",
object="chat.completion",
usage=openai.types.CompletionUsage(
prompt_tokens=10,
completion_tokens=20,
total_tokens=30,
),
)
def inner(
response_id: str,
response_model: str,
message_content: str,
created: int,
usage: openai.types.CompletionUsage,
):
return openai.types.chat.ChatCompletion(
id=response_id,
choices=[
openai.types.chat.chat_completion.Choice(
index=0,
finish_reason="stop",
message=openai.types.chat.ChatCompletionMessage(
role="assistant", content=message_content
),
)
],
created=created,
model=response_model,
object="chat.completion",
usage=usage,
)

return inner


@pytest.fixture
Expand Down
63 changes: 63 additions & 0 deletions tests/integrations/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
)

LANGCHAIN_VERSION = package_version("langchain")
LANGCHAIN_OPENAI_VERSION = package_version("langchain-openai")


@tool
Expand Down Expand Up @@ -170,6 +171,68 @@ def test_langchain_text_completion(
assert llm_span["data"]["gen_ai.usage.output_tokens"] == 15


def test_langchain_chat_with_run_name(
sentry_init,
capture_events,
get_model_response,
nonstreaming_chat_completions_model_response,
):
sentry_init(
integrations=[
LangchainIntegration(
include_prompts=True,
)
],
traces_sample_rate=1.0,
send_default_pii=True,
)
events = capture_events()

request_headers = {}
# Changed in https://github.com/langchain-ai/langchain/pull/32655
if LANGCHAIN_OPENAI_VERSION >= (0, 3, 32):
Comment thread
alexander-alderman-webb marked this conversation as resolved.
request_headers["X-Stainless-Raw-Response"] = "True"

model_response = get_model_response(
nonstreaming_chat_completions_model_response(
response_id="chat-id",
response_model="response-model-id",
message_content="the model response",
created=10000000,
usage=CompletionUsage(
prompt_tokens=20,
completion_tokens=10,
total_tokens=30,
),
),
serialize_pydantic=True,
request_headers=request_headers,
)

llm = ChatOpenAI(
model_name="gpt-3.5-turbo",
temperature=0,
openai_api_key="badkey",
)

with patch.object(
llm.client._client._client,
"send",
return_value=model_response,
) as _:
with start_transaction():
llm.invoke(
"How many letters in the word eudca",
config={"run_name": "my-snazzy-pipeline"},
)

tx = events[0]

chat_spans = list(x for x in tx["spans"] if x["op"] == "gen_ai.chat")
assert len(chat_spans) == 1
assert chat_spans[0]["data"][SPANDATA.GEN_AI_FUNCTION_ID] == "my-snazzy-pipeline"


@pytest.mark.skipif(
LANGCHAIN_VERSION < (1,),
reason="LangChain 1.0+ required (ONE AGENT refactor)",
Expand Down
Loading
Loading