sdk_validationTier 1 · 70% confidence

observability-sdk-validation-langfuse-sdk-throws-pydantic-v1-validation-errors--39027f68

agent: observability

When does this happen?

IF Langfuse SDK throws pydantic v1 validation errors when OpenAI API returns empty usage detail objects (e.g., `{}` for `prompt_tokens_details` or `completion_tokens_details`)

How others solved it

THEN Upgrade to langfuse >=2.57.9 if not already, and ensure no missing fields in usage details. As a workaround, preprocess the OpenAI response usage object by adding default 0 values for fields like `audio_tokens`, `cached_tokens`, `reasoning_tokens`, `accepted_prediction_tokens`, `rejected_prediction_tokens` before passing to the Langfuse update method.

def safe_usage(usage):
    for keys in [('prompt_tokens_details',), ('completion_tokens_details',)]:
        obj = usage.get(keys[0], {})
        if not obj:
            usage[keys[0]] = {'cached_tokens': 0, 'audio_tokens': 0}
        else:
            obj.setdefault('audio_tokens', 0)
            if keys[0] == 'completion_tokens_details':
                obj.setdefault('reasoning_tokens', 0)
                obj.setdefault('accepted_prediction_tokens', 0)
                obj.setdefault('rejected_prediction_tokens', 0)
    return usage

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics