Skip to content

Commit a266fde

Browse files
authored
perf(scores): build create_score body without Fern's dict() (#2006)
1 parent 4aa69a3 commit a266fde

3 files changed

Lines changed: 95 additions & 30 deletions

File tree

‎langfuse/_client/client.py‎

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
AsyncLangfuseAPI,
9999
CreateChatPromptRequest,
100100
CreateChatPromptType,
101-
CreateScoreRequest,
102101
CreateTextPromptRequest,
103102
Dataset,
104103
DatasetItem,
@@ -1946,29 +1945,26 @@ def create_score(
19461945
timestamp = timestamp.replace(tzinfo=timezone.utc)
19471946

19481947
try:
1949-
request = CreateScoreRequest(
1950-
id=score_id,
1951-
session_id=session_id,
1952-
dataset_run_id=dataset_run_id,
1953-
trace_id=trace_id,
1954-
observation_id=observation_id,
1955-
name=name,
1956-
value=value,
1957-
data_type=data_type, # type: ignore
1958-
comment=comment,
1959-
config_id=config_id,
1960-
environment=environment or self._environment,
1961-
timestamp=timestamp or _get_timestamp(),
1962-
)
1963-
# Fern's dict() keeps fields explicitly set to None, so drop them here.
1948+
# The model turned bools and ints into floats (True -> 1.0); keep that.
1949+
if isinstance(value, (bool, int)):
1950+
value = float(value)
1951+
19641952
score = {
1965-
key: value
1966-
for key, value in request.dict(by_alias=True).items()
1967-
if value is not None
1953+
"id": score_id,
1954+
"traceId": trace_id,
1955+
"sessionId": session_id,
1956+
"observationId": observation_id,
1957+
"datasetRunId": dataset_run_id,
1958+
"name": name,
1959+
"value": value,
1960+
"comment": comment,
1961+
"environment": environment or self._environment,
1962+
"dataType": data_type,
1963+
"configId": config_id,
1964+
"timestamp": timestamp or _get_timestamp(),
1965+
"metadata": metadata,
19681966
}
1969-
# The API accepts any JSON value as metadata, the generated model only dicts.
1970-
if metadata is not None:
1971-
score["metadata"] = metadata
1967+
score = {key: field for key, field in score.items() if field is not None}
19721968

19731969
if self._resources is not None:
19741970
# Force the score to be in sample if it was for a legacy trace ID, i.e. non-32 hexchar

‎tests/unit/test_resource_manager.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ def test_at_fork_reinit_new_httpx_client_uses_configured_timeout_and_headers(
409409
client.shutdown()
410410

411411

412-
def test_create_score_with_invalid_input_logs_error_without_enqueueing(
413-
monkeypatch, caplog
414-
):
412+
def test_create_score_logs_error_instead_of_raising(monkeypatch, caplog):
415413
monkeypatch.setenv("LANGFUSE_MEDIA_UPLOAD_ENABLED", "false")
416414

417415
with LangfuseResourceManager._lock:
@@ -424,13 +422,15 @@ def test_create_score_with_invalid_input_logs_error_without_enqueueing(
424422
)
425423
rm = client._resources
426424
assert rm is not None
427-
enqueued = []
428-
monkeypatch.setattr(rm, "add_score_task", lambda event, **_: enqueued.append(event))
425+
426+
def fail(event, **_):
427+
raise RuntimeError("enqueue failed")
428+
429+
monkeypatch.setattr(rm, "add_score_task", fail)
429430

430431
with caplog.at_level(logging.ERROR, logger="langfuse"):
431-
client.create_score(name="invalid", value=object(), trace_id="a" * 32)
432+
client.create_score(name="invalid", value=1.0, trace_id="a" * 32)
432433

433-
assert enqueued == []
434434
assert rm._score_queue.empty()
435435
error_records = [r for r in caplog.records if r.levelno == logging.ERROR]
436436
assert len(error_records) == 1

‎tests/unit/test_score_consumer.py‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from langfuse._client.resource_manager import LangfuseResourceManager
1919
from langfuse._task_manager import score_consumer as score_consumer_module
2020
from langfuse._task_manager.score_consumer import ScoreConsumer
21-
from langfuse.api import LangfuseAPI
21+
from langfuse._utils.serializer import EventSerializer
22+
from langfuse.api import CreateScoreRequest, LangfuseAPI
2223

2324
ACCEPTED = (202, {"message": "Accepted"})
2425

@@ -288,6 +289,8 @@ def client_factory():
288289
def create(recorder: Recorder, **kwargs: Any) -> Langfuse:
289290
with LangfuseResourceManager._lock:
290291
LangfuseResourceManager._instances.clear()
292+
# The global provider may carry a sampler set by another test.
293+
kwargs.setdefault("tracer_provider", TracerProvider())
291294
client = Langfuse(
292295
public_key="pk-score-consumer",
293296
secret_key="sk-score-consumer",
@@ -348,6 +351,72 @@ def test_create_score_posts_to_scores_endpoint(client_factory):
348351
assert sent[1]["timestamp"].endswith("Z")
349352

350353

354+
def test_create_score_payload_matches_request_model_serialization(client_factory):
355+
recorder = Recorder(_responder(ACCEPTED))
356+
client = client_factory(recorder, environment="staging")
357+
timestamp = datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
358+
cases: List[Dict[str, Any]] = [
359+
dict(
360+
name="numeric",
361+
value=0.9,
362+
trace_id="a" * 32,
363+
observation_id="b" * 16,
364+
data_type="NUMERIC",
365+
comment="fine",
366+
),
367+
dict(name="int-value", value=1, trace_id="a" * 32),
368+
dict(name="boolean", value=True, trace_id="a" * 32, data_type="BOOLEAN"),
369+
dict(
370+
name="categorical",
371+
value="good",
372+
session_id="session-1",
373+
data_type="CATEGORICAL",
374+
config_id="config-1",
375+
),
376+
dict(
377+
name="text",
378+
value="free text",
379+
dataset_run_id="run-1",
380+
data_type="TEXT",
381+
environment="override",
382+
),
383+
dict(
384+
name="correction",
385+
value="corrected output",
386+
trace_id="a" * 32,
387+
observation_id="b" * 16,
388+
data_type="CORRECTION",
389+
),
390+
dict(
391+
name="dict-metadata",
392+
value=0.5,
393+
trace_id="a" * 32,
394+
metadata={"evaluator": "judge", "nested": {"steps": [1, 2]}},
395+
),
396+
dict(name="list-metadata", value=0.5, trace_id="a" * 32, metadata=["a", 1]),
397+
]
398+
399+
for i, case in enumerate(cases):
400+
client.create_score(score_id=f"score-{i}", timestamp=timestamp, **case)
401+
client.flush()
402+
403+
expected = []
404+
for i, case in enumerate(cases):
405+
fields = dict(case)
406+
metadata = fields.pop("metadata", None)
407+
fields.setdefault("environment", "staging")
408+
request = CreateScoreRequest(id=f"score-{i}", timestamp=timestamp, **fields)
409+
payload = {
410+
k: v for k, v in request.dict(by_alias=True).items() if v is not None
411+
}
412+
if metadata is not None:
413+
payload["metadata"] = metadata
414+
expected.append(json.dumps(payload, cls=EventSerializer))
415+
# Compare JSON text, because 1 == 1.0 == True as Python values.
416+
sent = [json.dumps(score) for body in recorder.bodies for score in body]
417+
assert sent == expected
418+
419+
351420
@pytest.fixture
352421
def non_utc_local_timezone(monkeypatch):
353422
# Makes treating naive values as local time observable.

0 commit comments

Comments
 (0)