Skip to content
Open
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: 3 additions & 0 deletions py/src/braintrust/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4627,6 +4627,9 @@ def log_internal(self, event: dict[str, Any] | None = None, internal_data: dict[
metadata=serializable_partial_record.get("metadata"),
span_parents=self.span_parents,
span_attributes=serializable_partial_record.get("span_attributes"),
error=serializable_partial_record.get("error"),
metrics=serializable_partial_record.get("metrics"),
tags=serializable_partial_record.get("tags"),
)
self.state.span_cache.queue_write(self.root_span_id, self.span_id, cached_span)

Expand Down
17 changes: 16 additions & 1 deletion py/src/braintrust/span_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ def __init__(
metadata: Metadata | None = None,
span_parents: list[str] | None = None,
span_attributes: dict[str, Any] | None = None,
error: Any | None = None,
metrics: dict[str, Any] | None = None,
tags: list[str] | None = None,
):
self.span_id = span_id
self.input = input
self.output = output
self.metadata = metadata
self.span_parents = span_parents
self.span_attributes = span_attributes
self.error = error
self.metrics = metrics
self.tags = tags

def to_dict(self) -> dict[str, Any]:
"""Convert to dictionary for serialization."""
result = {"span_id": self.span_id}
result: dict[str, Any] = {"span_id": self.span_id}
if self.input is not None:
result["input"] = self.input
if self.output is not None:
Expand All @@ -54,6 +60,12 @@ def to_dict(self) -> dict[str, Any]:
result["span_parents"] = self.span_parents
if self.span_attributes is not None:
result["span_attributes"] = self.span_attributes
if self.error is not None:
result["error"] = self.error
if self.metrics is not None:
result["metrics"] = self.metrics
if self.tags is not None:
result["tags"] = self.tags
return result

@classmethod
Expand All @@ -66,6 +78,9 @@ def from_dict(cls, data: dict[str, Any]) -> "CachedSpan":
metadata=data.get("metadata"),
span_parents=data.get("span_parents"),
span_attributes=data.get("span_attributes"),
error=data.get("error"),
metrics=data.get("metrics"),
tags=data.get("tags"),
)


Expand Down
7 changes: 7 additions & 0 deletions py/src/braintrust/test_span_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def test_span_cache_write_and_read():
span_id="span-1",
input={"text": "hello"},
output={"response": "world"},
error={"message": "retryable"},
metrics={"start": 1, "end": 3},
tags=["production"],
)
span2 = CachedSpan(
span_id="span-2",
Expand All @@ -30,6 +33,10 @@ def test_span_cache_write_and_read():
span_ids = {s.span_id for s in spans}
assert "span-1" in span_ids
assert "span-2" in span_ids
stored_span1 = next(span for span in spans if span.span_id == "span-1")
assert stored_span1.error == {"message": "retryable"}
assert stored_span1.metrics == {"start": 1, "end": 3}
assert stored_span1.tags == ["production"]

cache.stop()
cache.dispose()
Expand Down
Loading