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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ module = [
"agents.*",
]

# openai library types differ between v1.99.9 (py39) and v2.7.2 (py313-latest),
# causing type ignores to be unused in one version but required in the other.
[[tool.mypy.overrides]]
module = "tests.test_span_attribute_helpers"
warn_unused_ignores = false

[tool.ruff]
line-length = 100
target-version = "py38"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,24 +361,14 @@ def _get_attributes_from_response_custom_tool_call_output_param(
yield f"{prefix}{MessageAttributes.MESSAGE_ROLE}", "tool"
if (call_id := obj.get("call_id")) is not None:
yield f"{prefix}{MessageAttributes.MESSAGE_TOOL_CALL_ID}", call_id
if (output := obj.get("output")) is not None:
if isinstance(output, str):
yield f"{prefix}{MESSAGE_CONTENT}", output
elif isinstance(output, list):
for i, item in enumerate(output):
if item["type"] == "input_text":
yield (
f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TEXT}",
item["text"] or "",
)
elif item["type"] == "input_image":
# TODO: handle the input image type for the tool input
pass
elif item["type"] == "input_file":
# TODO: handle the input file type for the tool input
pass
elif TYPE_CHECKING:
assert_never(item["type"])
if "output" in obj:
output = obj["output"]
if output is not None:
if isinstance(output, str):
output_value = output
else:
output_value = safe_json_dumps(output)
yield f"{prefix}{MessageAttributes.MESSAGE_CONTENT}", output_value


def _get_attributes_from_function_call_output(
Expand All @@ -387,24 +377,13 @@ def _get_attributes_from_function_call_output(
) -> Iterator[tuple[str, AttributeValue]]:
yield f"{prefix}{MESSAGE_ROLE}", "tool"
yield f"{prefix}{MESSAGE_TOOL_CALL_ID}", obj["call_id"]
if (output := obj.get("output")) is not None:
output = obj["output"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing key existence check for output field

The function directly accesses obj["output"] without checking if the key exists, which could raise a KeyError if the field is optional. The old code used obj.get("output") for safe access, and the similar function _get_attributes_from_response_custom_tool_call_output_param checks if "output" in obj: before accessing. This inconsistency could cause runtime errors if FunctionCallOutput ever has a missing output key.

Fix in Cursor Fix in Web

if output is not None:
if isinstance(output, str):
yield f"{prefix}{MESSAGE_CONTENT}", output
elif isinstance(output, list):
for i, item in enumerate(output):
if item["type"] == "input_text":
yield (
f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TEXT}",
item["text"] or "",
)
elif item["type"] == "input_image":
# TODO: handle the input image type for the tool input
pass
elif item["type"] == "input_file":
# TODO: handle the input file type for the tool input
pass
elif TYPE_CHECKING:
assert_never(item["type"])
output_value = output
else:
output_value = safe_json_dumps(output)
yield f"{prefix}{MESSAGE_CONTENT}", output_value


def _get_attributes_from_generation_span_data(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
ResponseUsage,
Tool,
)
from openai.types.responses.response_custom_tool_call_output_param import (
ResponseCustomToolCallOutputParam,
)
from openai.types.responses.response_function_web_search_param import ActionSearch
from openai.types.responses.response_input_item_param import (
ComputerCallOutput,
Expand Down Expand Up @@ -265,6 +268,51 @@
},
id="item_reference",
),
pytest.param(
[
ResponseCustomToolCallOutputParam(
type="custom_tool_call_output",
call_id="custom-123",
output="simple result",
)
],
{
"llm.input_messages.1.message.content": "simple result",
"llm.input_messages.1.message.role": "tool",
"llm.input_messages.1.message.tool_call_id": "custom-123",
},
id="custom_tool_call_output_string",
),
pytest.param(
[
ResponseCustomToolCallOutputParam(
type="custom_tool_call_output",
call_id="custom-123",
output=["item1", "item2"], # type: ignore[typeddict-item,list-item]
)
],
{
"llm.input_messages.1.message.content": '["item1", "item2"]',
"llm.input_messages.1.message.role": "tool",
"llm.input_messages.1.message.tool_call_id": "custom-123",
},
id="custom_tool_call_output_list",
),
pytest.param(
[
ResponseCustomToolCallOutputParam(
type="custom_tool_call_output",
call_id="custom-123",
output={"status": "success", "data": 42}, # type: ignore
)
],
{
"llm.input_messages.1.message.content": '{"status": "success", "data": 42}',
"llm.input_messages.1.message.role": "tool",
"llm.input_messages.1.message.tool_call_id": "custom-123",
},
id="custom_tool_call_output_dict",
),
],
)
def test_get_attributes_from_input(
Expand Down Expand Up @@ -409,8 +457,8 @@ def test_get_attributes_from_response_function_tool_call_param(
"output": "",
},
{
"message.role": "tool",
"message.content": "",
"message.role": "tool",
"message.tool_call_id": "123",
},
id="empty_output",
Expand All @@ -429,14 +477,26 @@ def test_get_attributes_from_response_function_tool_call_param(
pytest.param(
{
"call_id": "123",
"output": [{"type": "input_text", "text": "result"}],
"output": [{"type": "text", "text": "result"}],
},
{
"message.content": '[{"type": "text", "text": "result"}]',
"message.role": "tool",
"message.tool_call_id": "123",
},
id="list_output",
),
pytest.param(
{
"call_id": "123",
"output": {"result": "success", "value": 42},
},
{
"message.content": '{"result": "success", "value": 42}',
"message.role": "tool",
"message.contents.0.message_content.text": "result",
"message.tool_call_id": "123",
},
id="functional_call_output",
id="dict_output",
),
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
"opentelemetry-instrumentation",
"opentelemetry-semantic-conventions",
"openinference-instrumentation>=0.1.27",
"openinference-semantic-conventions>=0.1.23",
"openinference-semantic-conventions>=0.1.25",
"typing-extensions",
"wrapt",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
ToolCallAttributes,
)

# TODO: Update to use SpanAttributes.EMBEDDING_INVOCATION_PARAMETERS after
# https://github.com/Arize-ai/openinference/pull/2162 is merged
_EMBEDDING_INVOCATION_PARAMETERS = "embedding.invocation_parameters"

if TYPE_CHECKING:
from openai.types import Completion, CreateEmbeddingResponse
from openai.types.chat import ChatCompletion
Expand Down Expand Up @@ -253,7 +249,7 @@ def _get_attributes_from_embedding_create_param(
return
invocation_params = dict(params)
invocation_params.pop("input", None)
yield _EMBEDDING_INVOCATION_PARAMETERS, safe_json_dumps(invocation_params)
yield SpanAttributes.EMBEDDING_INVOCATION_PARAMETERS, safe_json_dumps(invocation_params)

# Extract text from embedding input - only records text, not token IDs
embedding_input = params.get("input")
Expand Down