Skip to content

Commit 3f35ff6

Browse files
committed
task(logger): update types / param docs for log_feedback and experiments
1 parent 84a9c07 commit 3f35ff6

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

py/src/braintrust/logger.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,7 +3391,7 @@ def _log_feedback_impl(
33913391
expected: Any | None = None,
33923392
tags: Sequence[str] | None = None,
33933393
comment: str | None = None,
3394-
metadata: Metadata | None = None,
3394+
metadata: object | None = None,
33953395
source: Literal["external", "app", "api", None] = None,
33963396
):
33973397
if source is None:
@@ -3880,7 +3880,7 @@ def log_feedback(
38803880
expected: Any | None = None,
38813881
tags: Sequence[str] | None = None,
38823882
comment: str | None = None,
3883-
metadata: Metadata | None = None,
3883+
metadata: object | None = None,
38843884
source: Literal["external", "app", "api", None] = None,
38853885
) -> None:
38863886
"""
@@ -3891,7 +3891,7 @@ def log_feedback(
38913891
:param expected: (Optional) the ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not.
38923892
:param tags: (Optional) a list of strings that you can use to filter and group records later.
38933893
:param comment: (Optional) an optional comment string to log about the event.
3894-
:param metadata: (Optional) a dictionary with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI. Note, this metadata does not correspond to the main event itself, but rather the audit log attached to the event.
3894+
:param metadata: (Optional) a dictionary, or an object that serializes to a dictionary, with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI. Note, this metadata does not correspond to the main event itself, but rather the audit log attached to the event. The values in `metadata` can be any JSON-serializable type, but its keys must be strings.
38953895
:param source: (Optional) the source of the feedback. Must be one of "external" (default), "app", or "api".
38963896
"""
38973897
return _log_feedback_impl(
@@ -5312,7 +5312,7 @@ def log_feedback(
53125312
expected: Any | None = None,
53135313
tags: Sequence[str] | None = None,
53145314
comment: str | None = None,
5315-
metadata: Metadata | None = None,
5315+
metadata: object | None = None,
53165316
source: Literal["external", "app", "api", None] = None,
53175317
) -> None:
53185318
"""
@@ -5323,7 +5323,7 @@ def log_feedback(
53235323
:param expected: (Optional) the ground truth value (an arbitrary, JSON serializable object) that you'd compare to `output` to determine if your `output` value is correct or not.
53245324
:param tags: (Optional) a list of strings that you can use to filter and group records later.
53255325
:param comment: (Optional) an optional comment string to log about the event.
5326-
:param metadata: (Optional) a dictionary with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI. Note, this metadata does not correspond to the main event itself, but rather the audit log attached to the event.
5326+
:param metadata: (Optional) a dictionary, or an object that serializes to a dictionary, with additional data about the feedback. If you have a `user_id`, you can log it here and access it in the Braintrust UI. Note, this metadata does not correspond to the main event itself, but rather the audit log attached to the event. The values in `metadata` can be any JSON-serializable type, but its keys must be strings.
53275327
:param source: (Optional) the source of the feedback. Must be one of "external" (default), "app", or "api".
53285328
"""
53295329
return _log_feedback_impl(

py/src/braintrust/test_logger.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
init_logger,
2323
logger,
2424
)
25+
from braintrust.db_fields import AUDIT_METADATA_FIELD
2526
from braintrust.id_gen import OTELIDGenerator, get_id_generator
2627
from braintrust.logger import (
2728
RemoteEvalParameters,
@@ -858,22 +859,66 @@ class MetadataModel(BaseModel):
858859
assert logs[0]["metadata"] == {"foo": "bar"}
859860

860861

861-
def test_span_log_accepts_model_dump_metadata(with_memory_logger):
862-
class MetadataModel:
863-
def model_dump(self, **kwargs):
864-
assert kwargs == {"exclude_none": True}
865-
return {"foo": "bar"}
862+
class _ModelDumpMetadata:
863+
def __init__(self, **values):
864+
self.values = values
865+
866+
def model_dump(self, **kwargs):
867+
assert kwargs == {"exclude_none": True}
868+
return dict(self.values)
869+
866870

871+
def test_span_log_accepts_model_dump_metadata(with_memory_logger):
867872
logger = init_test_logger(__name__)
868873

869874
with logger.start_span(name="test_span") as span:
870-
span.log(metadata=MetadataModel())
875+
span.log(metadata=_ModelDumpMetadata(foo="bar"))
876+
877+
logs = with_memory_logger.pop()
878+
assert len(logs) == 1
879+
assert logs[0]["metadata"] == {"foo": "bar"}
880+
881+
882+
def test_logger_log_accepts_model_dump_metadata(with_memory_logger):
883+
logger = init_test_logger(__name__)
884+
885+
logger.log(input="input", output="output", metadata=_ModelDumpMetadata(foo="bar"))
886+
887+
logs = with_memory_logger.pop()
888+
assert len(logs) == 1
889+
assert logs[0]["metadata"] == {"foo": "bar"}
890+
891+
892+
def test_experiment_log_accepts_model_dump_metadata(with_memory_logger):
893+
experiment = init_test_exp("test-experiment", "test-project")
894+
895+
experiment.log(input="input", output="output", scores={"score": 1}, metadata=_ModelDumpMetadata(foo="bar"))
871896

872897
logs = with_memory_logger.pop()
873898
assert len(logs) == 1
874899
assert logs[0]["metadata"] == {"foo": "bar"}
875900

876901

902+
def test_logger_log_feedback_accepts_model_dump_metadata(with_memory_logger):
903+
logger = init_test_logger(__name__)
904+
905+
logger.log_feedback(id="event-id", scores={"score": 1}, metadata=_ModelDumpMetadata(user_id="user-1"))
906+
907+
logs = with_memory_logger.pop()
908+
assert len(logs) == 1
909+
assert logs[0][AUDIT_METADATA_FIELD] == {"user_id": "user-1"}
910+
911+
912+
def test_experiment_log_feedback_accepts_model_dump_metadata(with_memory_logger):
913+
experiment = init_test_exp("test-experiment", "test-project")
914+
915+
experiment.log_feedback(id="event-id", scores={"score": 1}, metadata=_ModelDumpMetadata(user_id="user-1"))
916+
917+
logs = with_memory_logger.pop()
918+
assert len(logs) == 1
919+
assert logs[0][AUDIT_METADATA_FIELD] == {"user_id": "user-1"}
920+
921+
877922
def test_span_log_rejects_metadata_with_non_string_keys(with_memory_logger):
878923
logger = init_test_logger(__name__)
879924

@@ -2976,12 +3021,13 @@ def test_update_span_includes_span_id_and_root_span_id_from_export(with_memory_l
29763021

29773022
with_memory_logger.pop()
29783023

2979-
braintrust.update_span(exported=exported, output="updated output")
3024+
braintrust.update_span(exported=exported, output="updated output", metadata=_ModelDumpMetadata(foo="bar"))
29803025

29813026
logs = with_memory_logger.pop()
29823027
updated_log = next(log for log in logs if log.get("output") == "updated output")
29833028
assert updated_log["span_id"] == span_id
29843029
assert updated_log["root_span_id"] == root_span_id
3030+
assert updated_log["metadata"] == {"foo": "bar"}
29853031

29863032

29873033
def test_get_exporter_returns_v3_by_default():

0 commit comments

Comments
 (0)