Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/xai_sdk/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,11 @@ def _make_span_request_attributes(self) -> dict[str, Any]: # noqa: C901, PLR091
"""Creates a dictionary with all relevant request attributes to be set on the span as it is created."""
attributes: dict[str, Any] = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": self._proto.model,
"server.port": 443,
"server.address": "api.x.ai",
}

if should_disable_sensitive_attributes():
Expand Down
33 changes: 22 additions & 11 deletions tests/aio/chat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ async def test_sample_creates_span_with_correct_attributes(mock_tracer: mock.Mag

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -660,6 +660,7 @@ async def test_sample_creates_span_with_correct_attributes(mock_tracer: mock.Mag
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -709,10 +710,11 @@ async def test_sample_creates_span_without_sensitive_attributes_when_disabled(

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"server.port": 443,
"server.address": "api.x.ai",
}

mock_tracer.start_as_current_span.assert_called_once_with(
Expand Down Expand Up @@ -765,7 +767,7 @@ async def test_sample_creates_span_with_correct_optional_attributes(mock_tracer:

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "json_object",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": True,
Expand All @@ -774,6 +776,7 @@ async def test_sample_creates_span_with_correct_optional_attributes(mock_tracer:
"gen_ai.request.temperature": 0.5,
"gen_ai.request.parallel_tool_calls": False,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.request.max_tokens": 100,
"gen_ai.request.seed": 123,
Expand Down Expand Up @@ -815,7 +818,7 @@ async def test_sample_batch_creates_span_with_correct_attributes(mock_tracer: mo

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -824,6 +827,7 @@ async def test_sample_batch_creates_span_with_correct_attributes(mock_tracer: mo
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -882,7 +886,7 @@ async def test_stream_creates_span_with_correct_attributes(mock_tracer: mock.Mag

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -891,6 +895,7 @@ async def test_stream_creates_span_with_correct_attributes(mock_tracer: mock.Mag
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -947,7 +952,7 @@ async def test_stream_batch_creates_span_with_correct_attributes(mock_tracer: mo

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -956,6 +961,7 @@ async def test_stream_batch_creates_span_with_correct_attributes(mock_tracer: mo
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -1019,7 +1025,7 @@ class TestResponse(BaseModel):

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "json_schema",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1028,6 +1034,7 @@ class TestResponse(BaseModel):
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "What's the weather in London?",
Expand Down Expand Up @@ -1073,7 +1080,7 @@ async def test_defer_creates_span_with_correct_attributes(mock_tracer: mock.Magi

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1082,6 +1089,7 @@ async def test_defer_creates_span_with_correct_attributes(mock_tracer: mock.Magi
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -1131,7 +1139,7 @@ async def test_defer_batch_creates_span_with_correct_attributes(mock_tracer: moc

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1140,6 +1148,7 @@ async def test_defer_batch_creates_span_with_correct_attributes(mock_tracer: moc
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -1200,7 +1209,7 @@ async def test_chat_with_function_calling_creates_span_with_correct_attributes(

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1209,6 +1218,7 @@ async def test_chat_with_function_calling_creates_span_with_correct_attributes(
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "What's the weather in London?",
Expand Down Expand Up @@ -1281,7 +1291,7 @@ async def test_chat_with_function_call_result_creates_span_with_correct_attribut

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1290,6 +1300,7 @@ async def test_chat_with_function_call_result_creates_span_with_correct_attribut
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "What's the weather in London?",
Expand Down
33 changes: 22 additions & 11 deletions tests/sync/chat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def test_sample_creates_span_with_correct_attributes(mock_tracer: mock.MagicMock

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -633,6 +633,7 @@ def test_sample_creates_span_with_correct_attributes(mock_tracer: mock.MagicMock
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -679,10 +680,11 @@ def test_sample_creates_span_without_sensitive_attributes_when_disabled(mock_tra

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"server.port": 443,
"server.address": "api.x.ai",
}

mock_tracer.start_as_current_span.assert_called_once_with(
Expand Down Expand Up @@ -735,7 +737,7 @@ def test_sample_creates_span_with_correct_optional_attributes(mock_tracer: mock.

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "json_object",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": True,
Expand All @@ -744,6 +746,7 @@ def test_sample_creates_span_with_correct_optional_attributes(mock_tracer: mock.
"gen_ai.request.temperature": 0.5,
"gen_ai.request.parallel_tool_calls": False,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.request.max_tokens": 100,
"gen_ai.request.seed": 123,
Expand Down Expand Up @@ -784,7 +787,7 @@ def test_sample_batch_creates_span_with_correct_attributes(mock_tracer: mock.Mag

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -793,6 +796,7 @@ def test_sample_batch_creates_span_with_correct_attributes(mock_tracer: mock.Mag
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -851,7 +855,7 @@ def test_stream_creates_span_with_correct_attributes(mock_tracer: mock.MagicMock

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -860,6 +864,7 @@ def test_stream_creates_span_with_correct_attributes(mock_tracer: mock.MagicMock
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -916,7 +921,7 @@ def test_stream_batch_creates_span_with_correct_attributes(mock_tracer: mock.Mag

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -925,6 +930,7 @@ def test_stream_batch_creates_span_with_correct_attributes(mock_tracer: mock.Mag
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -985,7 +991,7 @@ class TestResponse(BaseModel):

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "json_schema",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -994,6 +1000,7 @@ class TestResponse(BaseModel):
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "What's the weather in London?",
Expand Down Expand Up @@ -1038,7 +1045,7 @@ def test_defer_creates_span_with_correct_attributes(mock_tracer: mock.MagicMock,

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1047,6 +1054,7 @@ def test_defer_creates_span_with_correct_attributes(mock_tracer: mock.MagicMock,
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -1095,7 +1103,7 @@ def test_defer_batch_creates_span_with_correct_attributes(mock_tracer: mock.Magi

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1104,6 +1112,7 @@ def test_defer_batch_creates_span_with_correct_attributes(mock_tracer: mock.Magi
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "Hello, how are you?",
Expand Down Expand Up @@ -1161,7 +1170,7 @@ def test_chat_with_function_calling_creates_span_with_correct_attributes(mock_tr

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1170,6 +1179,7 @@ def test_chat_with_function_calling_creates_span_with_correct_attributes(mock_tr
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "What's the weather in London?",
Expand Down Expand Up @@ -1241,7 +1251,7 @@ def test_chat_with_function_call_result_creates_span_with_correct_attributes(

expected_request_attributes = {
"gen_ai.operation.name": "chat",
"gen_ai.system": "xai",
"gen_ai.provider.name": "xai",
"gen_ai.output.type": "text",
"gen_ai.request.model": "grok-3",
"gen_ai.request.logprobs": False,
Expand All @@ -1250,6 +1260,7 @@ def test_chat_with_function_call_result_creates_span_with_correct_attributes(
"gen_ai.request.temperature": 1.0,
"gen_ai.request.parallel_tool_calls": True,
"server.port": 443,
"server.address": "api.x.ai",
"gen_ai.conversation.id": conversation_id,
"gen_ai.prompt.0.role": "user",
"gen_ai.prompt.0.content": "What's the weather in London?",
Expand Down