-
Couldn't load subscription status.
- Fork 2k
Implement FastAPI middleware solution for Weave async context propagation #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add comprehensive Weave integration to track AI-powered UI generation: Key changes: - Extract LLM logic into @weave.op() decorated function for proper tracing - Set WEAVE_PRINT_CALL_LINK=true to enable console trace output - Initialize Weave context within traced function for FastAPI compatibility - Add development documentation and POC example Technical details: 1. FastAPI decorator conflict: Separated AI logic from route handler 2. Missing environment variable: Set WEAVE_PRINT_CALL_LINK for console output 3. Context initialization: Call weave.init() within function for proper async/FastAPI tracing This enables full observability of UI generation requests with clickable trace links (🍩) in dev mode logs, matching weave_poc.py behavior. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
…tion - Add WeaveContextMiddleware to propagate Weave context across async requests - Remove weave.init() overhead from generate_ui_completion function - Update lifespan to initialize Weave once at server startup - Add comprehensive Playwright testing guide to CLAUDE.md - Document the implemented solution and remove outdated workaround notes - Create async_issues.md with detailed problem analysis and solution architecture Tested successfully: Weave trace URLs now generate without per-request initialization overhead. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Document complete test procedure to verify middleware works - Include success/failure indicators with exact log patterns - Add debugging steps for troubleshooting trace issues - Provide clear verification that context propagation is working 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
|
Caution Review the following alerts detected in dependencies. According to your organization's Security Policy, you must resolve all "Block" alerts before proceeding. It is recommended to resolve "Warn" alerts too. Learn more about Socket for GitHub.
|
Summary
The initial codebase had no Weave tracing applied to the async
chat_completions()function as would be expected for monitoring LLM interactions. When implementing@weave.op()decorators to enable tracing, we discovered significant issues with Weave context propagation in FastAPI async environments that prevented proper trace generation.This PR resolves these issues by implementing a robust FastAPI middleware solution that enables proper Weave tracing of async chat completions.
Key Changes
contextvars.copy_context().envfile loading on startup (no manualsource .envneeded)weave.init()overhead fromgenerate_ui_completionTechnical Details
Problem Discovered: FastAPI request handlers run in isolated async contexts where context variables set by the original
weave.init()at server startup are not available, causing@weave.op()decorators to fail silently.Solution: The middleware copies the synchronous Weave context (established at startup) into each asynchronous FastAPI request context, ensuring proper trace propagation.
Benefits
weave.init()calls@weave.op()decorated functionssource .envrequiredVerification
Successfully tested with evidence of proper trace generation:
Files Changed
openui/server.py: Added WeaveContextMiddleware and lifespan Weave initializationopenui/__main__.py: Enhanced dev mode Weave initializationCLAUDE.md: Comprehensive testing documentation and technical notes.env.example: Template for environment configurationasync_issues.md: Detailed problem analysis and solution architectureThis addresses a sophisticated async context issue common to FastAPI + tracing library combinations and provides the proper architectural solution for robust Weave tracing of chat completions.
🤖 Generated with Claude Code