Skip to content

Commit 43a49d5

Browse files
committed
cleanup comments
1 parent 220137b commit 43a49d5

File tree

1 file changed

+81
-102
lines changed

1 file changed

+81
-102
lines changed

packages/opentelemetry-instrumentation-langchain/tests/test_tool_call_content.py

Lines changed: 81 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -17,166 +17,145 @@ def test_assistant_message_with_tool_calls_includes_content():
1717
"""
1818
Test that when an assistant message has both content and tool_calls,
1919
both the content and tool_calls are included in the span attributes.
20-
20+
2121
This addresses the issue where content was missing when tool_calls were present.
2222
"""
23-
# Create a mock span
2423
mock_span = Mock()
2524
mock_span.set_attribute = Mock()
26-
27-
# Create a mock span_holder
2825
mock_span_holder = Mock()
2926
mock_span_holder.request_model = None
30-
31-
# Create messages that reproduce the issue:
32-
# 1. User message
33-
# 2. Assistant message with BOTH content AND tool_calls
34-
messages = [[
35-
HumanMessage(content="what is the current time? First greet me."),
36-
AIMessage(
37-
content="Hello! Let me check the current time for you.",
38-
tool_calls=[{
39-
'id': 'call_qU7pH3EdQvzwkPyKPOdpgaKA',
40-
'name': 'get_current_time',
41-
'args': {}
42-
}]
43-
),
44-
ToolMessage(
45-
content="2025-08-15 08:15:21",
46-
tool_call_id="call_qU7pH3EdQvzwkPyKPOdpgaKA"
47-
),
48-
AIMessage(content="The current time is 2025-08-15 08:15:21")
49-
]]
50-
51-
# Call the function that was previously buggy
27+
messages = [
28+
[
29+
HumanMessage(content="what is the current time? First greet me."),
30+
AIMessage(
31+
content="Hello! Let me check the current time for you.",
32+
tool_calls=[
33+
{
34+
"id": "call_qU7pH3EdQvzwkPyKPOdpgaKA",
35+
"name": "get_current_time",
36+
"args": {},
37+
}
38+
],
39+
),
40+
ToolMessage(
41+
content="2025-08-15 08:15:21",
42+
tool_call_id="call_qU7pH3EdQvzwkPyKPOdpgaKA",
43+
),
44+
AIMessage(content="The current time is 2025-08-15 08:15:21"),
45+
]
46+
]
47+
5248
set_chat_request(mock_span, {}, messages, {}, mock_span_holder)
53-
54-
# Verify that set_attribute was called with the expected attributes
49+
5550
call_args = [call[0] for call in mock_span.set_attribute.call_args_list]
56-
57-
# Extract all attribute names and values
5851
attributes = {args[0]: args[1] for args in call_args}
59-
60-
# Check user message (prompt.0)
52+
6153
assert f"{SpanAttributes.LLM_PROMPTS}.0.role" in attributes
6254
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "user"
6355
assert f"{SpanAttributes.LLM_PROMPTS}.0.content" in attributes
64-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"] == "what is the current time? First greet me."
65-
66-
# Check assistant message with tool calls (prompt.1)
67-
# This is the critical test - BOTH content AND tool_calls should be present
56+
assert (
57+
attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"]
58+
== "what is the current time? First greet me."
59+
)
6860
assert f"{SpanAttributes.LLM_PROMPTS}.1.role" in attributes
6961
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.1.role"] == "assistant"
70-
71-
# The fix should ensure that content is present even when tool_calls exist
7262
assert f"{SpanAttributes.LLM_PROMPTS}.1.content" in attributes
73-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.1.content"] == "Hello! Let me check the current time for you."
74-
75-
# Tool calls should also be present
63+
assert (
64+
attributes[f"{SpanAttributes.LLM_PROMPTS}.1.content"]
65+
== "Hello! Let me check the current time for you."
66+
)
7667
assert f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.id" in attributes
77-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.id"] == "call_qU7pH3EdQvzwkPyKPOdpgaKA"
78-
assert f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.name" in attributes
79-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.name"] == "get_current_time"
80-
81-
# Check tool message (prompt.2)
68+
assert (
69+
attributes[f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.id"]
70+
== "call_qU7pH3EdQvzwkPyKPOdpgaKA"
71+
)
72+
assert f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.name" in attributes
73+
assert (
74+
attributes[f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.name"]
75+
== "get_current_time"
76+
)
8277
assert f"{SpanAttributes.LLM_PROMPTS}.2.role" in attributes
8378
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.2.role"] == "tool"
8479
assert f"{SpanAttributes.LLM_PROMPTS}.2.content" in attributes
85-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.2.content"] == "2025-08-15 08:15:21"
80+
assert (
81+
attributes[f"{SpanAttributes.LLM_PROMPTS}.2.content"] == "2025-08-15 08:15:21"
82+
)
8683
assert f"{SpanAttributes.LLM_PROMPTS}.2.tool_call_id" in attributes
87-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.2.tool_call_id"] == "call_qU7pH3EdQvzwkPyKPOdpgaKA"
88-
89-
# Check final assistant message (prompt.3)
84+
assert (
85+
attributes[f"{SpanAttributes.LLM_PROMPTS}.2.tool_call_id"]
86+
== "call_qU7pH3EdQvzwkPyKPOdpgaKA"
87+
)
9088
assert f"{SpanAttributes.LLM_PROMPTS}.3.role" in attributes
9189
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.3.role"] == "assistant"
9290
assert f"{SpanAttributes.LLM_PROMPTS}.3.content" in attributes
93-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.3.content"] == "The current time is 2025-08-15 08:15:21"
91+
assert (
92+
attributes[f"{SpanAttributes.LLM_PROMPTS}.3.content"]
93+
== "The current time is 2025-08-15 08:15:21"
94+
)
9495

9596

9697
def test_assistant_message_with_only_tool_calls_no_content():
9798
"""
9899
Test that when an assistant message has only tool_calls and no content,
99100
the tool_calls are still included and no content attribute is set.
100101
"""
101-
# Create a mock span
102102
mock_span = Mock()
103103
mock_span.set_attribute = Mock()
104-
105-
# Create a mock span_holder
106104
mock_span_holder = Mock()
107105
mock_span_holder.request_model = None
108-
109-
# Create message with only tool_calls, no content
110-
messages = [[
111-
AIMessage(
112-
content="", # Empty content
113-
tool_calls=[{
114-
'id': 'call_123',
115-
'name': 'some_tool',
116-
'args': {'param': 'value'}
117-
}]
118-
)
119-
]]
120-
121-
# Call the function
106+
107+
messages = [
108+
[
109+
AIMessage(
110+
content="",
111+
tool_calls=[
112+
{"id": "call_123", "name": "some_tool", "args": {"param": "value"}}
113+
],
114+
)
115+
]
116+
]
117+
122118
set_chat_request(mock_span, {}, messages, {}, mock_span_holder)
123-
124-
# Verify that set_attribute was called with the expected attributes
119+
125120
call_args = [call[0] for call in mock_span.set_attribute.call_args_list]
126-
127-
# Extract all attribute names and values
128121
attributes = {args[0]: args[1] for args in call_args}
129-
130-
# Check assistant message
122+
131123
assert f"{SpanAttributes.LLM_PROMPTS}.0.role" in attributes
132124
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "assistant"
133-
134-
# Content should NOT be set when it's empty (due to _set_span_attribute logic)
135-
# This is expected behavior to avoid cluttering spans with empty values
136125
assert f"{SpanAttributes.LLM_PROMPTS}.0.content" not in attributes
137-
138-
# Tool calls should be present
139126
assert f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.id" in attributes
140127
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.id"] == "call_123"
141128
assert f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.name" in attributes
142-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.name"] == "some_tool"
129+
assert (
130+
attributes[f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.name"] == "some_tool"
131+
)
143132

144133

145134
def test_assistant_message_with_only_content_no_tool_calls():
146135
"""
147136
Test that when an assistant message has only content and no tool_calls,
148137
the content is included and no tool_calls attributes are set.
149138
"""
150-
# Create a mock span
151139
mock_span = Mock()
152140
mock_span.set_attribute = Mock()
153-
154-
# Create a mock span_holder
155141
mock_span_holder = Mock()
156142
mock_span_holder.request_model = None
157-
158-
# Create message with only content, no tool_calls
159-
messages = [[
160-
AIMessage(content="Just a regular response with no tool calls")
161-
]]
162-
163-
# Call the function
143+
144+
messages = [[AIMessage(content="Just a regular response with no tool calls")]]
145+
164146
set_chat_request(mock_span, {}, messages, {}, mock_span_holder)
165-
166-
# Verify that set_attribute was called with the expected attributes
147+
167148
call_args = [call[0] for call in mock_span.set_attribute.call_args_list]
168-
169-
# Extract all attribute names and values
149+
170150
attributes = {args[0]: args[1] for args in call_args}
171-
172-
# Check assistant message
151+
173152
assert f"{SpanAttributes.LLM_PROMPTS}.0.role" in attributes
174153
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "assistant"
175-
176-
# Content should be present
177154
assert f"{SpanAttributes.LLM_PROMPTS}.0.content" in attributes
178-
assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"] == "Just a regular response with no tool calls"
179-
180-
# No tool call attributes should be present
155+
assert (
156+
attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"]
157+
== "Just a regular response with no tool calls"
158+
)
159+
181160
tool_call_attributes = [attr for attr in attributes.keys() if "tool_calls" in attr]
182-
assert len(tool_call_attributes) == 0
161+
assert len(tool_call_attributes) == 0

0 commit comments

Comments
 (0)