fix: Correct the sessionId parameter in ScoreBody when calling langfuse.create_score(...)#1580
Conversation
parameter in ScoreBody call
| new_body = ScoreBody( | ||
| id=score_id, | ||
| session_id=session_id, | ||
| sessionId=session_id, |
There was a problem hiding this comment.
Fix looks correct — alias required without
populate_by_name
ScoreBody.session_id is declared with FieldMetadata(alias="sessionId"), and ScoreBody.model_config does not include populate_by_name=True. In Pydantic v2, without that flag, the constructor only accepts the alias form. Passing session_id=session_id (the Python field name) was silently stored as an extra field (because extra="allow" is set on ScoreBody) instead of populating the intended session_id / sessionId field — meaning sessionId was always sent as null to the API.
The change to sessionId=session_id is consistent with every other aliased field in the same constructor call (traceId, observationId, datasetRunId, dataType, configId) and is the correct fix.
|
Thanks for raising this, this has been fixed already on main 👍🏾 |
Fixes langfuse/langfuse#12812
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a one-line bug in
Langfuse.create_score()whereScoreBodywas constructed with the Python field namesession_id=instead of the required Pydantic aliassessionId=.Why the original code was broken:
ScoreBody.session_idis declared asAnnotated[Optional[str], FieldMetadata(alias="sessionId")].ScoreBody.model_configdoes not setpopulate_by_name=True.__init__. Becauseextra="allow"is also set onScoreBody, passingsession_id=valuewas silently stored as an extra field rather than setting the actual field — sosessionIdwas always serialised asnullin the ingestion payload.What the fix does:
session_id=session_id→sessionId=session_id, bringing it in line with all other aliased fields in the same constructor call (traceId,observationId,datasetRunId,dataType,configId).The change is minimal, targeted, and correct with no side effects.
Confidence Score: 5/5
Safe to merge — single-line targeted bug fix with no behavioural changes beyond correcting the missing
sessionIdfield in score ingestion payloads.The change is a one-line fix that corrects a clear Pydantic alias misuse. It matches the established pattern of every other field in the same constructor call, has no impact on any other code path, and directly resolves the linked issue.
No files require special attention.
Important Files Changed
session_id=tosessionId=when constructingScoreBody, making it consistent with every other aliased field and correctly populating the Pydantic alias field.Sequence Diagram
sequenceDiagram participant Caller participant Langfuse participant ScoreBody participant IngestionQueue Caller->>Langfuse: create_score(session_id=..., trace_id=..., name=..., value=...) Langfuse->>ScoreBody: ScoreBody(sessionId=session_id, traceId=trace_id, ...) Note over ScoreBody: Pydantic validates via alias<br/>(sessionId, not session_id) ScoreBody-->>Langfuse: validated body Langfuse->>IngestionQueue: add_score_task({type: "score-create", body: ScoreBody}) IngestionQueue-->>Caller: (async, no return value)Reviews (1): Last reviewed commit: "Fix langfuse.create_score() by replacing..." | Re-trigger Greptile
(2/5) Greptile learns from your feedback when you react with thumbs up/down!