- 
                Notifications
    
You must be signed in to change notification settings  - Fork 14
 
Implement lilypad.configure({ auto_llm: true }) for OpenAI in Lilypad TypeScript SDK #601
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: update-typescript-sdk
Are you sure you want to change the base?
Conversation
…ilypad TypeScript SDK
…e unused EventSummaryResponse
… with dom.iterable
…lidation constants
          Codecov ReportAttention: Patch coverage is  
 Additional details and impacted files@@                    Coverage Diff                     @@
##           update-typescript-sdk     #601       +/-   ##
==========================================================
- Coverage                  99.92%   89.74%   -10.18%     
==========================================================
  Files                        190      211       +21     
  Lines                      10115    13372     +3257     
  Branches                       0      226      +226     
==========================================================
+ Hits                       10107    12001     +1894     
- Misses                         8     1370     +1362     
- Partials                       0        1        +1     
 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
  | 
    
…es, and enhance span type definitions
…lypad TypeScript SDK
…variable handling
…variable handling
… and error handling
…ling and module export checks
| # AutoLLM Import Order Constraint | ||
| 
               | 
          ||
| ## Important Limitation | ||
| 
               | 
          ||
| When using the `autoLlm: true` option, there is a critical constraint regarding module import order: | ||
| 
               | 
          ||
| ### ❌ This will NOT work: | ||
| ```typescript | ||
| import OpenAI from "openai"; // OpenAI imported BEFORE configure() | ||
| import { configure } from "lilypad"; | ||
| 
               | 
          ||
| await configure({ | ||
| autoLlm: true // Hook cannot intercept already-loaded module | ||
| }); | ||
| 
               | 
          ||
| const client = new OpenAI(); // Will NOT be instrumented | ||
| ``` | ||
| 
               | 
          ||
| ### ✅ This WILL work: | ||
| ```typescript | ||
| import { configure } from "lilypad"; | ||
| 
               | 
          ||
| // Configure FIRST | ||
| await configure({ | ||
| autoLlm: true | ||
| }); | ||
| 
               | 
          ||
| // Import OpenAI AFTER configure() | ||
| const { default: OpenAI } = await import("openai"); | ||
| const client = new OpenAI(); // Will be instrumented correctly | ||
| ``` | ||
| 
               | 
          ||
| ## Technical Explanation | ||
| 
               | 
          ||
| The `autoLlm` feature uses Node.js `Module._load` hooks to intercept and wrap the OpenAI module at load time. This approach has an inherent limitation: | ||
| 
               | 
          ||
| - **Module._load hooks only work when a module is loaded for the first time** | ||
| - If the module is already in the require cache, the hook will not be triggered | ||
| - This is a fundamental constraint of Node.js module system, not specific to our implementation | ||
| 
               | 
          ||
| ## Recommended Approaches | ||
| 
               | 
          ||
| ### 1. Use Dynamic Import (Most Flexible) | ||
| ```typescript | ||
| await configure({ autoLlm: true }); | ||
| const { default: OpenAI } = await import("openai"); | ||
| ``` | ||
| 
               | 
          ||
| ### 2. Use --require Flag (Most Reliable) | ||
| ```bash | ||
| node --require ./dist/register.js your-app.js | ||
| ``` | ||
| This ensures hooks are installed before any application code runs. | ||
| 
               | 
          ||
| ### 3. Manual Wrapping (Most Control) | ||
| ```typescript | ||
| import OpenAI from "openai"; | ||
| import { wrapOpenAI } from "lilypad"; | ||
| 
               | 
          ||
| const client = wrapOpenAI(new OpenAI()); | ||
| ``` | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brenkao @willbakst
Unlike Python, there appear to be limitations when applying patches (module hooks). If you know of a solution, please share it.
We should also decide and document which approach to recommend as a library before publishing the package. Related to this, I've discovered that things become even more complicated when parent spans exist, so I'll comment on that in a separate PR.
…environment variable cleanup
feat: add span and session functionality with type definitions
No description provided.