feat: add langfuse openai SDK tracing to openai_provider only#3433
feat: add langfuse openai SDK tracing to openai_provider only#3433leesf wants to merge 15 commits intoagentscope-ai:mainfrom
Conversation
Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/866badcb-6274-4c83-8e0e-38cbec1ea35a Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/c126b162-3248-4e4e-a1f0-a78488042930 Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
… not installed Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/9b75df92-92e2-4a75-b51c-44c9199d116e Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/69d05c99-82cf-45f2-ad1b-dde39b93e7a0 Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
feat(observability): add optional Langfuse tracing for provider-backed LLM calls
|
Hi @leesf, thank you for your first Pull Request! 🎉 📋 About PR TemplateTo help maintainers review your PR faster, please make sure to include:
Complete PR information helps speed up the review process. You can edit the PR description to add these details. 🙌 Join Developer CommunityThanks 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: We truly appreciate your enthusiasm—and look forward to your future contributions! 😊 We'll review your PR soon. |
There was a problem hiding this comment.
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.AsyncOpenAIandopenai.AsyncOpenAI. - Added an import-time warning when
LANGFUSE_SECRET_KEYis 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.
| 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" | ||
| ) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
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
AsyncOpenAIfromlangfuse.openaiwhen Langfuse tracing is configured. - Falls back to
openai.AsyncOpenAIand logs a warning when tracing is partially configured. - Adds
osandimportlib.utilimports to support env/package detection.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@leesf Please take a look at the Copilot comments and make modifications if you find them helpful. |
|
And please format the code by |
Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/daf93103-1af3-464d-8080-1ef1afcdedb8 Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/daf93103-1af3-464d-8080-1ef1afcdedb8 Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
Fix `pre-commit run --all-files` failures in OpenAI provider import/type wiring
… import Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/e0dcd6e5-ed11-4cbc-9d2e-a7d5a4ff058c Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/959b4d27-8043-49a5-a557-941a69e4021f Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
…port Agent-Logs-Url: https://github.com/leesf/QwenPaw/sessions/959b4d27-8043-49a5-a557-941a69e4021f Co-authored-by: leesf <10128888+leesf@users.noreply.github.com>
refactor: replace AsyncOpenAIType with direct AsyncOpenAI conditional import
Thanks, updated the PR. |
There was a problem hiding this comment.
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.AsyncOpenAIvsopenai.AsyncOpenAI. - Added warning logging when
LANGFUSE_SECRET_KEYis set butlangfuseis not installed. - Added
importlib.utilandosimports to support the conditional behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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 |

This pull request improves the integration with Langfuse tracing in the
OpenAIProviderby conditionally importing the appropriateAsyncOpenAIclient 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:
AsyncOpenAInow checks ifLANGFUSE_SECRET_KEYis set and if thelangfusepackage is installed; if both are true, it importsAsyncOpenAIfromlangfuse.openai, otherwise it falls back to the standardopenaiclient.LANGFUSE_SECRET_KEYis set butlangfuseis not installed, a warning is logged to inform the user how to enable tracing.Dependency management:
importlib.utilandosto support the conditional logic for importing and environment variable checking.