Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 47e55e0

Browse files
authored
Merge pull request #67 from Chainlit/willy/make-peerdeps-optional
fix: require instrumentation dependencies at runtime
2 parents 7351c3f + bc5df84 commit 47e55e0

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

src/instrumentation/index.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import OpenAI from 'openai';
2-
31
import { LiteralClient, Maybe } from '..';
4-
import { LiteralCallbackHandler } from './langchain';
5-
import { instrumentLlamaIndex, withThread } from './llamaindex';
6-
import instrumentOpenAI from './openai';
7-
import { makeInstrumentVercelSDK } from './vercel-sdk';
2+
import { Thread } from '../observability/thread';
3+
import type { AllVercelFn } from './vercel-sdk';
84

95
export type OpenAIGlobalOptions = {
106
tags?: Maybe<string[]>;
117
metadata?: Maybe<Record<string, any>>;
12-
client?: OpenAI;
8+
client?: any;
139
};
1410

1511
export default (client: LiteralClient) => ({
@@ -23,14 +19,26 @@ export default (client: LiteralClient) => ({
2319
* @param options.metadata Metadata to attach to all generations.
2420
* @returns
2521
*/
26-
openai: (options?: OpenAIGlobalOptions) => instrumentOpenAI(client, options),
22+
openai: (options?: OpenAIGlobalOptions) => {
23+
try {
24+
// eslint-disable-next-line @typescript-eslint/no-var-requires
25+
const { default: instrumentOpenAI } = require('./openai');
26+
return instrumentOpenAI(client, options);
27+
} catch (error) {
28+
throw new Error(
29+
'Failed to load OpenAI. Please ensure openai is installed.'
30+
);
31+
}
32+
},
2733

2834
langchain: {
2935
literalCallback: (params?: {
3036
threadId?: string;
3137
chainTypesToIgnore?: string[];
3238
}) => {
3339
try {
40+
// eslint-disable-next-line @typescript-eslint/no-var-requires
41+
const { LiteralCallbackHandler } = require('./langchain');
3442
return new LiteralCallbackHandler(
3543
client,
3644
params?.threadId,
@@ -45,23 +53,41 @@ export default (client: LiteralClient) => ({
4553
},
4654

4755
vercel: {
48-
/**
49-
* Instrument a Vercel SDK function to log all invocations to the Literal AI API.
50-
* It will be augmented with a `literalAiParent` option that allows you to specify a parent step or thread.
51-
* @param fn The function to instrument. This can be Vercel SDK's `generateText` or `streamText` functions.
52-
* @returns A new function that logs all invocations to the Literal AI API.
53-
*/
54-
instrument: makeInstrumentVercelSDK(client)
56+
instrument: <TFunction extends AllVercelFn>(fn: TFunction) => {
57+
try {
58+
// eslint-disable-next-line @typescript-eslint/no-var-requires
59+
const { makeInstrumentVercelSDK } = require('./vercel-sdk');
60+
return makeInstrumentVercelSDK(client)(fn);
61+
} catch (error) {
62+
throw new Error(
63+
'Failed to load Vercel SDK. Please ensure @vercel/ai is installed.'
64+
);
65+
}
66+
}
5567
},
5668

5769
llamaIndex: {
58-
/**
59-
* Instrument the LlamaIndex client to log all generations to the Literal AI API.
60-
*/
61-
instrument: () => instrumentLlamaIndex(client),
62-
/**
63-
* Run a callback with a thread context.
64-
*/
65-
withThread
70+
instrument: () => {
71+
try {
72+
// eslint-disable-next-line @typescript-eslint/no-var-requires
73+
const { instrumentLlamaIndex } = require('./llamaindex');
74+
return instrumentLlamaIndex(client);
75+
} catch (error) {
76+
throw new Error(
77+
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
78+
);
79+
}
80+
},
81+
withThread: <R>(thread: Thread, callback: () => R) => {
82+
try {
83+
// eslint-disable-next-line @typescript-eslint/no-var-requires
84+
const { withThread } = require('./llamaindex');
85+
return withThread(thread, callback);
86+
} catch (error) {
87+
throw new Error(
88+
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
89+
);
90+
}
91+
}
6692
}
6793
});

src/instrumentation/vercel-sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Result<T extends (...args: any[]) => any> = Awaited<ReturnType<T>>;
2626

2727
type GenerateFn = typeof generateObject | typeof generateText;
2828
type StreamFn = typeof streamObject | typeof streamText;
29-
type AllVercelFn = GenerateFn | StreamFn;
29+
export type AllVercelFn = GenerateFn | StreamFn;
3030

3131
type OriginalStreamPart = string | ObjectStreamPart<any> | TextStreamPart<any>;
3232

@@ -289,7 +289,7 @@ export type InstrumentationVercelMethod = {
289289
options: Parameters<typeof streamText<TOOLS>>[0] & VercelExtraOptions
290290
) => ReturnType<typeof streamText<TOOLS>>;
291291

292-
(fn: typeof generateObject): <T>(
292+
(fn: typeof generateObject): <T extends string>(
293293
options: Parameters<typeof generateObject<T>>[0] & VercelExtraOptions
294294
) => ReturnType<typeof generateObject<T>>;
295295

tests/integration/vercel-sdk.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe.skip('Vercel SDK Instrumentation', () => {
257257
parameters: z.object({
258258
value: z.number().describe('The value in celsius')
259259
}),
260-
execute: async ({ value }) => {
260+
execute: async ({ value }: any) => {
261261
const celsius = parseFloat(value);
262262
const fahrenheit = celsius * (9 / 5) + 32;
263263
return fahrenheit;
@@ -345,7 +345,7 @@ describe.skip('Vercel SDK Instrumentation', () => {
345345
parameters: z.object({
346346
value: z.number().describe('The value in celsius')
347347
}),
348-
execute: async ({ value }) => {
348+
execute: async ({ value }: any) => {
349349
const celsius = parseFloat(value);
350350
const fahrenheit = celsius * (9 / 5) + 32;
351351
return fahrenheit;

0 commit comments

Comments
 (0)