Skip to content

Commit

Permalink
attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
axiomofjoy committed Jan 21, 2025
1 parent a4c699f commit 05d0f81
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ def set_input(
*,
mime_type: Optional[OpenInferenceMimeType] = None,
) -> None:
if OPENINFERENCE_SPAN_KIND not in self._self_important_attributes:
raise ValueError("Cannot set input attributes on a non-OpenInference span")
self.set_attributes(get_input_attributes(value, mime_type=mime_type))

def set_output(
Expand All @@ -558,6 +560,8 @@ def set_output(
*,
mime_type: Optional[OpenInferenceMimeType] = None,
) -> None:
if OPENINFERENCE_SPAN_KIND not in self._self_important_attributes:
raise ValueError("Cannot set output attributes on a non-OpenInference span")
self.set_attributes(get_output_attributes(value, mime_type=mime_type))

def set_tool(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,42 @@ def test_set_tool_attributes_on_non_tool_span_raises_exception(
)
assert str(exc_info.value) == "Cannot set tool attributes on a non-tool span"

def test_non_openinference_span(
self,
in_memory_span_exporter: InMemorySpanExporter,
tracer: OITracer,
) -> None:
with tracer.start_as_current_span("non-openinference-span") as non_openinference_span:
non_openinference_span.set_attribute("custom.attribute", "value")

spans = in_memory_span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
assert span.name == "non-openinference-span"
attributes = dict(span.attributes or {})
assert attributes.pop("custom.attribute") == "value"
assert not attributes

def test_cannot_set_input_on_non_openinference_span(
self,
in_memory_span_exporter: InMemorySpanExporter,
tracer: OITracer,
) -> None:
with tracer.start_as_current_span("non-openinference-span") as span:
with pytest.raises(ValueError) as exc_info:
span.set_input("input")
assert str(exc_info.value) == "Cannot set input attributes on a non-OpenInference span"

def test_cannot_set_output_on_non_openinference_span(
self,
in_memory_span_exporter: InMemorySpanExporter,
tracer: OITracer,
) -> None:
with tracer.start_as_current_span("non-openinference-span") as span:
with pytest.raises(ValueError) as exc_info:
span.set_output("output")
assert str(exc_info.value) == "Cannot set output attributes on a non-OpenInference span"

def test_tool(
self,
in_memory_span_exporter: InMemorySpanExporter,
Expand Down

0 comments on commit 05d0f81

Please sign in to comment.