|
| 1 | +""" |
| 2 | +Test for the fix of the issue where assistant message content is missing |
| 3 | +when tool calls are present in LangGraph/LangChain instrumentation. |
| 4 | +
|
| 5 | +This test reproduces the issue reported in GitHub where gen_ai.prompt.X.content |
| 6 | +attributes were missing for assistant messages that contained tool_calls. |
| 7 | +""" |
| 8 | + |
| 9 | +from unittest.mock import Mock |
| 10 | +from langchain_core.messages import AIMessage, HumanMessage, ToolMessage |
| 11 | +from opentelemetry.instrumentation.langchain.span_utils import set_chat_request |
| 12 | +from opentelemetry.semconv_ai import SpanAttributes |
| 13 | + |
| 14 | + |
| 15 | +def test_assistant_message_with_tool_calls_includes_content(): |
| 16 | + """ |
| 17 | + Test that when an assistant message has both content and tool_calls, |
| 18 | + both the content and tool_calls are included in the span attributes. |
| 19 | +
|
| 20 | + This addresses the issue where content was missing when tool_calls were present. |
| 21 | + """ |
| 22 | + mock_span = Mock() |
| 23 | + mock_span.set_attribute = Mock() |
| 24 | + mock_span_holder = Mock() |
| 25 | + mock_span_holder.request_model = None |
| 26 | + messages = [ |
| 27 | + [ |
| 28 | + HumanMessage(content="what is the current time? First greet me."), |
| 29 | + AIMessage( |
| 30 | + content="Hello! Let me check the current time for you.", |
| 31 | + tool_calls=[ |
| 32 | + { |
| 33 | + "id": "call_qU7pH3EdQvzwkPyKPOdpgaKA", |
| 34 | + "name": "get_current_time", |
| 35 | + "args": {}, |
| 36 | + } |
| 37 | + ], |
| 38 | + ), |
| 39 | + ToolMessage( |
| 40 | + content="2025-08-15 08:15:21", |
| 41 | + tool_call_id="call_qU7pH3EdQvzwkPyKPOdpgaKA", |
| 42 | + ), |
| 43 | + AIMessage(content="The current time is 2025-08-15 08:15:21"), |
| 44 | + ] |
| 45 | + ] |
| 46 | + |
| 47 | + set_chat_request(mock_span, {}, messages, {}, mock_span_holder) |
| 48 | + |
| 49 | + call_args = [call[0] for call in mock_span.set_attribute.call_args_list] |
| 50 | + attributes = {args[0]: args[1] for args in call_args} |
| 51 | + |
| 52 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.role" in attributes |
| 53 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "user" |
| 54 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.content" in attributes |
| 55 | + assert ( |
| 56 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"] |
| 57 | + == "what is the current time? First greet me." |
| 58 | + ) |
| 59 | + assert f"{SpanAttributes.LLM_PROMPTS}.1.role" in attributes |
| 60 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.1.role"] == "assistant" |
| 61 | + assert f"{SpanAttributes.LLM_PROMPTS}.1.content" in attributes |
| 62 | + assert ( |
| 63 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.1.content"] |
| 64 | + == "Hello! Let me check the current time for you." |
| 65 | + ) |
| 66 | + assert f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.id" in attributes |
| 67 | + assert ( |
| 68 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.id"] |
| 69 | + == "call_qU7pH3EdQvzwkPyKPOdpgaKA" |
| 70 | + ) |
| 71 | + assert f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.name" in attributes |
| 72 | + assert ( |
| 73 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.1.tool_calls.0.name"] |
| 74 | + == "get_current_time" |
| 75 | + ) |
| 76 | + assert f"{SpanAttributes.LLM_PROMPTS}.2.role" in attributes |
| 77 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.2.role"] == "tool" |
| 78 | + assert f"{SpanAttributes.LLM_PROMPTS}.2.content" in attributes |
| 79 | + assert ( |
| 80 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.2.content"] == "2025-08-15 08:15:21" |
| 81 | + ) |
| 82 | + assert f"{SpanAttributes.LLM_PROMPTS}.2.tool_call_id" in attributes |
| 83 | + assert ( |
| 84 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.2.tool_call_id"] |
| 85 | + == "call_qU7pH3EdQvzwkPyKPOdpgaKA" |
| 86 | + ) |
| 87 | + assert f"{SpanAttributes.LLM_PROMPTS}.3.role" in attributes |
| 88 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.3.role"] == "assistant" |
| 89 | + assert f"{SpanAttributes.LLM_PROMPTS}.3.content" in attributes |
| 90 | + assert ( |
| 91 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.3.content"] |
| 92 | + == "The current time is 2025-08-15 08:15:21" |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def test_assistant_message_with_only_tool_calls_no_content(): |
| 97 | + """ |
| 98 | + Test that when an assistant message has only tool_calls and no content, |
| 99 | + the tool_calls are still included and no content attribute is set. |
| 100 | + """ |
| 101 | + mock_span = Mock() |
| 102 | + mock_span.set_attribute = Mock() |
| 103 | + mock_span_holder = Mock() |
| 104 | + mock_span_holder.request_model = None |
| 105 | + |
| 106 | + messages = [ |
| 107 | + [ |
| 108 | + AIMessage( |
| 109 | + content="", |
| 110 | + tool_calls=[ |
| 111 | + {"id": "call_123", "name": "some_tool", "args": {"param": "value"}} |
| 112 | + ], |
| 113 | + ) |
| 114 | + ] |
| 115 | + ] |
| 116 | + |
| 117 | + set_chat_request(mock_span, {}, messages, {}, mock_span_holder) |
| 118 | + |
| 119 | + call_args = [call[0] for call in mock_span.set_attribute.call_args_list] |
| 120 | + attributes = {args[0]: args[1] for args in call_args} |
| 121 | + |
| 122 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.role" in attributes |
| 123 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "assistant" |
| 124 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.content" not in attributes |
| 125 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.id" in attributes |
| 126 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.id"] == "call_123" |
| 127 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.name" in attributes |
| 128 | + assert ( |
| 129 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.0.tool_calls.0.name"] == "some_tool" |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +def test_assistant_message_with_only_content_no_tool_calls(): |
| 134 | + """ |
| 135 | + Test that when an assistant message has only content and no tool_calls, |
| 136 | + the content is included and no tool_calls attributes are set. |
| 137 | + """ |
| 138 | + mock_span = Mock() |
| 139 | + mock_span.set_attribute = Mock() |
| 140 | + mock_span_holder = Mock() |
| 141 | + mock_span_holder.request_model = None |
| 142 | + |
| 143 | + messages = [[AIMessage(content="Just a regular response with no tool calls")]] |
| 144 | + |
| 145 | + set_chat_request(mock_span, {}, messages, {}, mock_span_holder) |
| 146 | + |
| 147 | + call_args = [call[0] for call in mock_span.set_attribute.call_args_list] |
| 148 | + |
| 149 | + attributes = {args[0]: args[1] for args in call_args} |
| 150 | + |
| 151 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.role" in attributes |
| 152 | + assert attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"] == "assistant" |
| 153 | + assert f"{SpanAttributes.LLM_PROMPTS}.0.content" in attributes |
| 154 | + assert ( |
| 155 | + attributes[f"{SpanAttributes.LLM_PROMPTS}.0.content"] |
| 156 | + == "Just a regular response with no tool calls" |
| 157 | + ) |
| 158 | + |
| 159 | + tool_call_attributes = [attr for attr in attributes.keys() if "tool_calls" in attr] |
| 160 | + assert len(tool_call_attributes) == 0 |
0 commit comments