feat(client): Add optional timestamp parameter to score creation methods #1463
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.
Add optional timestamp parameter to score creation methods
This PR adds the ability to pass a custom
timestampparameter when creating scores, allowing users to backfill scores with historical timestamps or set specific evaluation times.Motivation
Currently, scores are always created with the current UTC timestamp via
_get_timestamp(). However, there are legitimate use cases where users need to specify a custom timestamp:Changes
This PR adds an optional
timestamp: Optional[datetime] = Noneparameter to:Langfuse.create_score()- Core score creation method inclient.pyLangfuseSpan.score()- Convenience method for scoring a specific spanLangfuseSpan.score_trace()- Convenience method for scoring an entire traceThe implementation follows existing conventions in the codebase (similar to
completion_start_timeparameters) and uses the patterntimestamp or _get_timestamp()to maintain backward compatibility.Example Usage
Important
Add optional
timestampparameter to score creation methods inclient.pyandspan.py, allowing custom timestamps for scores.timestampparameter toLangfuse.create_score(),LangfuseSpan.score(), andLangfuseSpan.score_trace()to allow custom timestamps for scores.timestampis not provided.client.py,create_score()method updated to accepttimestampand usetimestamp or _get_timestamp().span.py,score()andscore_trace()methods updated to accepttimestamp.test_create_score_with_custom_timestamp()intest_core_sdk.pyto verify custom timestamp functionality.This description was created by
for e158f0e. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
This PR adds an optional
timestampparameter to score creation methods, enabling users to backfill historical scores or set specific evaluation times.timestamp: Optional[datetime] = NonetoLangfuse.create_score(),LangfuseSpan.score(), andLangfuseSpan.score_trace()methodstimestamp or _get_timestamp()pattern for backward compatibilityConfidence Score: 5/5
Important Files Changed
File Analysis
timestampparameter tocreate_score()method and overloads, with proper fallback to_get_timestamp().timestampparameter toscore()andscore_trace()methods and overloads, passing through tocreate_score().Sequence Diagram
sequenceDiagram participant User participant Langfuse as Langfuse Client participant Span as LangfuseSpan participant ResourceMgr as Resource Manager User->>Langfuse: create_score(name, value, timestamp) Langfuse->>Langfuse: timestamp or _get_timestamp() Langfuse->>Langfuse: Build ScoreBody event Langfuse->>ResourceMgr: add_score_task(event) ResourceMgr-->>User: Score created Note over User,ResourceMgr: Alternative: Score via Span User->>Span: span.score(name, value, timestamp) Span->>Langfuse: create_score(trace_id, observation_id, timestamp) Langfuse->>ResourceMgr: add_score_task(event) ResourceMgr-->>User: Score created