Skip to content

feat: add langfuse openai SDK tracing to openai_provider only#3433

Open
leesf wants to merge 15 commits intoagentscope-ai:mainfrom
leesf:main
Open

feat: add langfuse openai SDK tracing to openai_provider only#3433
leesf wants to merge 15 commits intoagentscope-ai:mainfrom
leesf:main

Conversation

@leesf
Copy link
Copy Markdown

@leesf leesf commented Apr 15, 2026

This pull request improves the integration with Langfuse tracing in the OpenAIProvider by conditionally importing the appropriate AsyncOpenAI client based on environment variables and package availability. This ensures that Langfuse tracing is only enabled when both the secret key is set and the package is installed, and provides clear logging if the setup is incomplete.

Conditional Langfuse integration:

  • The import of AsyncOpenAI now checks if LANGFUSE_SECRET_KEY is set and if the langfuse package is installed; if both are true, it imports AsyncOpenAI from langfuse.openai, otherwise it falls back to the standard openai client.
  • If LANGFUSE_SECRET_KEY is set but langfuse is not installed, a warning is logged to inform the user how to enable tracing.

Dependency management:

  • Added imports for importlib.util and os to support the conditional logic for importing and environment variable checking.

Copilot AI and others added 7 commits April 15, 2026 06:34
Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
feat(observability): add optional Langfuse tracing for provider-backed LLM calls
Copilot AI review requested due to automatic review settings April 15, 2026 09:21
@github-project-automation github-project-automation bot moved this to Todo in QwenPaw Apr 15, 2026
@github-actions github-actions bot added the first-time-contributor PR created by a first time contributor label Apr 15, 2026
@github-actions
Copy link
Copy Markdown

Welcome to QwenPaw! 🐾

Hi @leesf, thank you for your first Pull Request! 🎉

📋 About PR Template

To help maintainers review your PR faster, please make sure to include:

  • Description - What this PR does and why
  • Type of Change - Bug fix / Feature / Breaking change / Documentation / Refactoring
  • Component(s) Affected - Core / Console / Channels / Skills / CLI / Documentation / Tests / CI/CD / Scripts
  • Checklist:
    • Run and pass pre-commit run --all-files
    • Run and pass relevant tests (pytest or as applicable)
    • Update documentation if needed
  • Testing - How to test these changes
  • Local Verification Evidence:
    pre-commit run --all-files
    # paste summary result
    
    pytest
    # paste summary result

Complete PR information helps speed up the review process. You can edit the PR description to add these details.

🙌 Join Developer Community

Thanks so much for your contribution! We'd love to invite you to join the official QwenPaw developer group! You can find the Discord and DingTalk group links under the "Developer Community" section on our docs page:
https://qwenpaw.agentscope.io/docs/community

We truly appreciate your enthusiasm—and look forward to your future contributions! 😊

We'll review your PR soon.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds optional Langfuse tracing support to the OpenAIProvider by selecting AsyncOpenAI from langfuse.openai only when LANGFUSE_SECRET_KEY is set and Langfuse is available, otherwise falling back to the standard OpenAI SDK client.

Changes:

  • Added conditional import logic to choose between langfuse.openai.AsyncOpenAI and openai.AsyncOpenAI.
  • Added an import-time warning when LANGFUSE_SECRET_KEY is set but Langfuse isn’t installed.
  • Added supporting imports (importlib.util, os) for environment/package checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +26 to +33
if os.environ.get("LANGFUSE_SECRET_KEY") and importlib.util.find_spec("langfuse"):
from langfuse.openai import AsyncOpenAI # type: ignore[import]
else:
if os.environ.get("LANGFUSE_SECRET_KEY"):
logging.warning(
"LANGFUSE_SECRET_KEY is set but langfuse is not installed; "
"install with `pip install langfuse` to enable tracing"
)
Comment thread src/qwenpaw/providers/openai_provider.py Outdated
Comment thread src/qwenpaw/providers/openai_provider.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 15, 2026 09:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an optional Langfuse-instrumented OpenAI client to OpenAIProvider so tracing is enabled only when LANGFUSE_SECRET_KEY is set and the Langfuse package is available, otherwise falling back to the standard OpenAI SDK with a warning.

Changes:

  • Conditionally imports AsyncOpenAI from langfuse.openai when Langfuse tracing is configured.
  • Falls back to openai.AsyncOpenAI and logs a warning when tracing is partially configured.
  • Adds os and importlib.util imports to support env/package detection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/qwenpaw/providers/openai_provider.py Outdated
@leesf leesf requested a deployment to maintainer-approved April 15, 2026 10:05 — with GitHub Actions Waiting
@xieyxclack
Copy link
Copy Markdown
Member

@leesf Please take a look at the Copilot comments and make modifications if you find them helpful.

@xieyxclack
Copy link
Copy Markdown
Member

And please format the code by pre-commit run --all-files

Copilot AI and others added 7 commits April 15, 2026 12:21
Fix `pre-commit run --all-files` failures in OpenAI provider import/type wiring
refactor: replace AsyncOpenAIType with direct AsyncOpenAI conditional import
Copilot AI review requested due to automatic review settings April 16, 2026 01:17
@leesf
Copy link
Copy Markdown
Author

leesf commented Apr 16, 2026

And please format the code by pre-commit run --all-files

Thanks, updated the PR.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds optional Langfuse tracing support to OpenAIProvider by conditionally importing AsyncOpenAI from langfuse.openai only when LANGFUSE_SECRET_KEY is set and Langfuse is available, otherwise falling back to the standard OpenAI SDK client and logging a warning when tracing is misconfigured.

Changes:

  • Added conditional import logic to select langfuse.openai.AsyncOpenAI vs openai.AsyncOpenAI.
  • Added warning logging when LANGFUSE_SECRET_KEY is set but langfuse is not installed.
  • Added importlib.util and os imports to support the conditional behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +26 to +36
if os.environ.get("LANGFUSE_SECRET_KEY") and importlib.util.find_spec(
"langfuse",
):
from langfuse.openai import AsyncOpenAI # type: ignore[import]
else:
if os.environ.get("LANGFUSE_SECRET_KEY"):
logger.warning(
"LANGFUSE_SECRET_KEY is set but langfuse is not installed; "
"install with `pip install langfuse` to enable tracing",
)
from openai import AsyncOpenAI # pylint: disable=ungrouped-imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-time-contributor PR created by a first time contributor Under Review

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants