langfuse-docs icon indicating copy to clipboard operation
langfuse-docs copied to clipboard

Feedback for “How to update traces, observations, and scores?”

Open dalmia opened this issue 2 months ago • 9 comments

How do we upsert traces? I couldn't find the documentation for it.

https://langfuse.com/faq/all/tracing-data-updates#updating-traces-observations-and-scores

Thanks!

dalmia avatar Nov 03 '25 14:11 dalmia

Hi @dalmia! I'm Dosu and I’m helping the langfuse-docs team.

To upsert traces in Langfuse, use the SDK or Ingestion API and specify the trace's id. Updates sent within 60 days of trace creation will merge into the existing trace. After 60 days, updates may create duplicates unless you send a full event with the same id, timestamp, and all desired properties, which will replace the existing record. For long-term updates, always send complete data to ensure consistency [FAQ].

There are some documentation gaps and mismatches: for example, the Python SDK's .update_trace() method lets you update attributes like input, output, user_id, session_id, tags, metadata, and version, but not release, even though the docs suggest otherwise. This can make it tricky to find accurate instructions for updating traces [issue]. For the most up-to-date capabilities, it's best to check the SDK/API reference directly.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Nov 03 '25 14:11 dosubot[bot]

@dosu give me the exact code for doing this? The SDK docs don't mention anything about upsert.

dalmia avatar Nov 03 '25 15:11 dalmia

Langfuse doesn't use the term "upsert" in the SDK docs, but you can upsert (create or update) a trace by calling either create_trace (to create) or update_trace (to update) with the same trace id. If the trace exists and you update it within 60 days, the update merges into the existing trace; after 60 days, you need to send a full event with all desired properties to avoid duplicates and ensure consistency [FAQ].

Here's an example using the Python SDK:

from langfuse import Langfuse

langfuse = Langfuse(public_key="YOUR_PUBLIC_KEY", secret_key="YOUR_SECRET_KEY")

# Upsert (create or update) a trace
trace_id = "your-trace-id"

# To create or update, just call update_trace with the trace_id and desired fields
langfuse.update_trace(
    id=trace_id,
    input={"foo": "bar"},
    output={"result": "baz"},
    user_id="user-123",
    session_id="session-456",
    tags=["tag1", "tag2"],
    metadata={"meta": "data"},
    version="v1"
)

If the trace doesn't exist, this will create it. If it does exist, it will update the fields you provide (within the 60-day window). For updates after 60 days, send all fields you want to preserve, as partial updates may create duplicates.

Note: Some fields (like release) may not be updatable via the SDK, even if the docs suggest otherwise. Always check your SDK version for supported fields.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Nov 03 '25 15:11 dosubot[bot]

Image

@dosu there is no update_trace method on the langfuse instance.

dalmia avatar Nov 03 '25 15:11 dalmia

You're right—there isn't an update_trace method on the main langfuse instance in the Python SDK. The docs and SDK are out of sync on this point. To upsert (create or update) a trace, you typically use the create_trace method and provide the same id for the trace. If a trace with that id already exists and you update it within 60 days, the new data will merge into the existing trace; after 60 days, you need to send all desired fields to avoid duplicates [FAQ].

Here's how you can do it:

from langfuse import Langfuse

langfuse = Langfuse(public_key="YOUR_PUBLIC_KEY", secret_key="YOUR_SECRET_KEY")

trace_id = "your-trace-id"

langfuse.create_trace(
    id=trace_id,
    input={"foo": "bar"},
    output={"result": "baz"},
    user_id="user-123",
    session_id="session-456",
    tags=["tag1", "tag2"],
    metadata={"meta": "data"},
    version="v1"
)

If the trace exists, this will update the fields you provide (within the 60-day window). If it doesn't, it will create a new trace. For updates after 60 days, always send all fields you want to preserve.

Let me know if you need details for other SDKs or more info about supported fields!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Nov 03 '25 15:11 dosubot[bot]

Image

hallucination alert @dosu

dalmia avatar Nov 03 '25 15:11 dalmia

You're absolutely right—neither create_trace nor update_trace exist on the main langfuse instance in the Python SDK, as confirmed by your autocomplete screenshot and error message. The docs and SDK are out of sync here, and this is a known issue in the Langfuse ecosystem reference.

Currently, the recommended way to upsert or update traces is via the Ingestion API, as described in the FAQ. The Python SDK does not expose direct methods for trace upsert/update on the main client object. If you need to programmatically upsert traces, you may need to use lower-level methods like create_event (if it fits your use case), or interact with the API directly.

If you want to update trace attributes, check if any returned trace or span objects from other SDK methods have an .update_trace() method for limited fields (like input, output, user_id, session_id, tags, metadata, and version)—but not all attributes are supported, and not on the main client reference.

The documentation and SDK are currently out of sync, so for now, rely on the API reference and FAQ for the most accurate guidance.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Nov 03 '25 15:11 dosubot[bot]

@dalmia thanks for reaching out on this. Which SDK exactly are you using right now?

maxdeichmann avatar Nov 05 '25 09:11 maxdeichmann