Skip to content

Bug: using_attributes loses span attributes in async functions #2009

Done

Description

@gtdd123

Component

OpenLIT Python SDK

What happened?

@openlit.using_attributes drops its configured attributes when it decorates an async def function. The exported score event still exists, but any grouping or lookup based on the missing field loses that context. Using the same API as a with block around an await preserves the attribute.

Steps to reproduce

Run this locally with the SDK installed. It uses an in-memory OpenTelemetry span and no provider or collector.

import asyncio

import openlit
from opentelemetry.sdk.trace import TracerProvider

openlit.OpenlitConfig.disable_events = True
tracer = TracerProvider().get_tracer("local-repro")


@openlit.using_attributes({"example.stage": "agent"})
async def score():
    await asyncio.sleep(0)
    with tracer.start_as_current_span("response") as span:
        assert openlit.log_score("quality", 1)
        return span.events[-1].attributes.get("example.stage")


result = asyncio.run(score())
print(f"example.stage: {result!r}")
# Current: None. Expected: "agent".

Expected behavior

The configured attributes are present while the async function runs, including after an await. The caller's previous attributes are restored when the function exits.

Environment

OpenLIT Python SDK 1.45.0, main 463a1c1b3cd72a94fab4155efe0f28d90d87fe95, Python 3.12.11, Windows.

Additional context

Codex assisted with source inspection and the reproduction. This is a local reproduction using synthetic score data.

Pre-submission checklist

  • I searched existing issues and didn't find a duplicate

Are you willing to submit PR?

Yes, I am willing to submit a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions