dify icon indicating copy to clipboard operation
dify copied to clipboard

feat: Add W&B Weave Tracing Integration

Open parambharat opened this issue 10 months ago • 2 comments

Summary

This PR adds W&B Weave as a new tracing provider in Dify, resolving the feature request in #14463. The integration follows similar patterns to the existing tracers, allowing users to track LLM calls, workflows, and tool usage in W&B Weave.

Motivation and Context

Many Dify users leverage Weights & Biases for their ML workflow tracking and would benefit from native W&B Weave integration. This addition gives users more flexibility in choosing tracing providers that align with their existing MLOps stack.

Weave offers specialized capabilities for LLM applications:

  • Detailed call tracing with input/output capture
  • Support for complex application flows
  • Rich visualization and analytics
  • Integration with evaluation frameworks

Implementation Details

The implementation:

  • Adds Weave as a new tracing provider option
  • Creates a WeaveConfig model for storing API key, entity, project, and endpoint configuration
  • Implements a client wrapper for W&B Weave client
  • Develops a tracer class implementing the BaseTraceInstance interface
  • Integrates with Dify's existing trace queue management system

Dependencies

  • Added dependency: weave package for Python SDK integration

The implementation follows Dify's existing patterns to ensure consistency while providing the new functionality needed for W&B Weave integration.

Resolves #14463

Checklist

  • [x] This change requires a documentation update, included: Dify Document :Docs PR

  • [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)

  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.

  • [x] I've updated the documentation accordingly.

  • [x] I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

parambharat avatar Feb 24 '25 10:02 parambharat

@parambharat hi! Friendly reminder that you'd need to resolve the merge conflicts before we get this PR out!

guchenhe avatar Mar 18 '25 23:03 guchenhe

@guchenhe : Apologies, I was on vacation for the last two weeks. I've addressed @aiswaryasankarwork's comments and also resolved the conflicts and pushed the changes.

parambharat avatar Mar 31 '25 15:03 parambharat

Hi @aiswaryasankarwork . Pinging again to check if you got a chance to review this PR. Let me know if you need any changes if necessary.

parambharat avatar Apr 07 '25 07:04 parambharat

Hi @guchenhe and @aiswaryasankarwork just a gentle reminder to take a look at this PR when you get a chance. It’s been open for a while. Would really appreciate your review. Thanks!

parambharat avatar Apr 21 '25 02:04 parambharat

Hi @guchenhe and @aiswaryasankarwork just a gentle reminder to take a look at this PR when you get a chance. It’s been open for a while. Would really appreciate your review. Thanks!

Hi @parambharat thanks alot for the PR! mind adding a few type annotations so it passes mypy typechecking in our CI?

guchenhe avatar Apr 21 '25 08:04 guchenhe

thanks @parambharat again for the fix - I went and tested it out a little, and it seems that when I tried to configure weave as the tracing provider on the popup: CleanShot 2025-04-21 at 19 58 11@2x

I don't get a response on the frontend, but instead was prompted on the backend command line to login: 2025-04-21 23:21:15,969 INFO [_internal.py:97] 127.0.0.1 - - [21/Apr/2025 23:21:15] "OPTIONS /console/api/apps/3c302975-bf4a-4926-a225-10f84a739d2d/trace-config HTTP/1.1" 200 - Please login to Weights & Biases (https://wandb.ai/) to continue: wandb: Logging into wandb.ai. (Learn how to deploy a W&B server locally: https://wandb.me/wandb-server) wandb: You can find your API key in your browser here: https://wandb.ai/authorize?ref=weave wandb: Paste an API key from your profile and hit enter, or press ctrl+c to quit:

In many scenarios, user who are making apps and configuring tracing may not have access to the backend console. since they would already enter their API key in the popup window, I wonder if you could kindly make the configuration process entirely through Dify's frontend instead :)

Additionally, I am not able to see traces logged to weave after configuring the tracing connection. My setup is in the screenshot and I configured a weave project named dify-tracing-test: CleanShot 2025-04-21 at 20 11 12@2x not sure if I did anything wrong and would appreciate if you could take a look.

Thanks so much and excited to get this out the door soon! 🫡

guchenhe avatar Apr 22 '25 00:04 guchenhe

hey @guchenhe, This was unexpected. However, I was able to pinpoint the issue and push a fix. Please take a look and let me know if the issue persists.

parambharat avatar Apr 23 '25 13:04 parambharat

hi @parambharat , can confirm the login issue has been fixed! Upon testing, I found that a call to a Dify chatflow app produces 2 traces in weave - for example, an invocation like so: CleanShot 2025-04-26 at 18 30 43@2x shows up as a workflow trace and a message trace in weave: CleanShot 2025-04-26 at 18 32 36@2x

Just wanted to double check whether this is expected behavior before we merge?

Attaching the full weave output fyi: weave_export_dify-weave-integration-test-2_2025-04-26 (1).csv

guchenhe avatar Apr 26 '25 10:04 guchenhe

Hi @guchenhe , yes that's the expected behaviour. Two traces are expected since they are created at different time periods apart. However, they can be grouped by session/trace_id.

parambharat avatar Apr 26 '25 10:04 parambharat

merged - thank you @parambharat!

guchenhe avatar Apr 26 '25 11:04 guchenhe