feat(scoring): add ScoreClient#create!, Client#create_score!, Langfuse.create_score! - #102
Open
Alan-Marx wants to merge 1 commit into
Open
Conversation
…e.create_score! Synchronous score creation that bypasses the in-memory queue and trace-sampling gate, sending immediately and raising on delivery failure — for a standalone score arriving out-of-band (e.g. user feedback) where the caller needs to know the outcome to decide whether to retry, unlike #create's fire-and-forget batching. Extracted the validation/normalization/event-building shared by #create and #create! into #build_score_event itself, rather than duplicating it across both. Also fixes handle_batch_response for the whole SDK, not just this new path: the ingestion endpoint's own spec says it never returns a 4xx for rejected events — "input errors" surface as a 200/207 response whose body lists them in an `errors` array instead, which is the only real per-event success/failure signal. handle_batch_response previously treated every 200/201/204/207 as unconditional success without ever parsing that array, so a rejected event was silently reported as delivered regardless of entry point.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DRAdds
ScoreClient#create!/Client#create_score!/Langfuse.create_score!for synchronous, exception-raising score creation, and fixes a latent bug where the SDK silently treated ingestion-rejected batch events as delivered.Whycreate_scoreis fire-and-forget: it queues the event and reports nothing about whether it was actually delivered, which matches how this SDK's tracing already works and is the right default for scoring inline from a still-open span. But it's the wrong shape for a standalone score arriving out-of-band — e.g. user feedback landing in a request unrelated to the turn it's scoring — where the caller needs to know whether delivery succeeded in order to decide whether to retry.create_score!bypasses the in-memory queue and the trace-sampling gate, sends immediately, and raises (UnauthorizedError/ApiError) on failure.While building that, found and fixed a bug in
handle_batch_responsethat affects the whole SDK, not just this new path: Langfuse's ingestion endpoint never returns a 4xx for rejected events — per the endpoint's own spec, "input errors" surface as a 200-series/207 response whose body lists them in anerrorsarray instead. That array is the only real per-event success/failure signal; HTTP status alone can't distinguish a fully accepted batch from a fully rejected one.handle_batch_responsepreviously treated every 200/201/204/207 as unconditional success without ever parsing that array, so a rejected event (e.g. failed validation) was silently reported as delivered. This matters more oncecreate_score!exists, since callers are now relying on "no exception" to mean "delivered" — but it was already wrong for every batch send before this PR.ChecklistCloses #
What changedLangfuse::ScoreClient#create!: new synchronous variant of#create— builds the same event, then callsapi_client.send_batch([event])directly instead of enqueueing.Langfuse::Client#create_score!andLangfuse.create_score!: flat-API forwarding wrappers, matching this SDK's no-nested-objects convention.data_typelookup) out of#createand into#build_score_eventitself, so#create!doesn't duplicate it.Langfuse::ApiClient#handle_batch_response: now callsraise_on_batch_errors, which parses the response body'serrorsarray and raisesApiErrorsummarizing the rejected event(s) if it's non-empty. Extractedparse_response_body(previously inlined inextract_error_message) since both now need it.docs/API_REFERENCE.md: documentedcreate_score!.CHANGELOG.md: noted under[Unreleased].ValidationNote
Medium Risk
Changes ingestion success semantics for all
send_batchcallers (queued score flushes can now surface API rejections that were previously ignored at the HTTP layer), though queued paths still swallow exceptions; the new API is intentionally strict for out-of-band scoring.Overview
Adds
create_score!onLangfuse,Client, andScoreClientfor scores that must be delivered synchronously (e.g. late user feedback): it skips the in-memory queue and trace sampling gate, calls ingestion immediately, and raisesUnauthorizedError/ApiErroron failure—unlike fire-and-forgetcreate_score.Score event building is consolidated so
#createand#create!sharebuild_score_event(name/value validation and normalization).ApiClient#handle_batch_responsenow treats Langfuse ingestion correctly: on 200/201/204/207 it inspects the response body’serrorsarray and raisesApiErrorwhen events were rejected, instead of treating every success HTTP status as delivered.parse_response_bodyis shared withextract_error_message.Reviewed by Cursor Bugbot for commit d48a9a3. Bugbot is set up for automated code reviews on this repo. Configure here.