Skip to content

Bug (Langfuse): _sanitize_usage_data unexpectedly flattens OpenAI-style usage format #2587

@LastRemote

Description

@LastRemote

Describe the bug
In langfuse-haystack>=3.0.0 we now use _sanitize_usage_data for better feeding into Langfuse specification. However, this unexpectedly flattens the usage data format in OpenAI style, e.g.:

{
    "completion_tokens": 1080,
    "completion_tokens_details": {
        "accepted_prediction_tokens": 0,
        "audio_tokens": 0,
        "reasoning_tokens": 0,
        "rejected_prediction_tokens": 0
    },
    "prompt_tokens": 4126,
    "prompt_tokens_details": {
        "audio_tokens": 0,
        "cached_tokens": 4096
    },
    "total_tokens": 5206
}

After the flattening, it seems like the data is no longer recognized by Langfuse, and it falls back to the default behavior, showing 0->0 (14508) instead.

Langfuse summarizes all usage types that include the string input as input usage types, similarly output as output usage types. If no total usage type is ingested, Langfuse sums up all usage type units to a total.

Image

Digging deeper inside Langfuse handling, it expects a yaml structure for OpenAI Usage as follows:

OpenAICompletionUsageSchema:
  docs: OpenAI Usage schema from (Chat-)Completion APIs
  properties:
    prompt_tokens: integer
    completion_tokens: integer
    total_tokens: integer
    prompt_tokens_details: optional<map<string, optional<integer>>>
    completion_tokens_details: optional<map<string, optional<integer>>>

To Reproduce
Run an OpenAI model in a pipeline and observe the trace.

Suggestion on Fixing
The easiest solution should be skip sanitizing if we found OpenAI-style usage pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions