Skip to content

Commit

Permalink
Fix tracing for streaming calls with no config
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Jul 20, 2024
1 parent 3a6b48e commit 609851f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 4 additions & 2 deletions langchain-core/src/runnables/iter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function* consumeIteratorInContext<T>(
while (true) {
const { value, done } = AsyncLocalStorageProviderSingleton.runWithConfig(
context,
iter.next.bind(iter)
iter.next.bind(iter),
true
);
if (done) {
break;
Expand All @@ -56,7 +57,8 @@ export async function* consumeAsyncIterableInContext<T>(
const { value, done } =
await AsyncLocalStorageProviderSingleton.runWithConfig(
context,
iterator.next.bind(iter)
iterator.next.bind(iter),
true
);
if (done) {
break;
Expand Down
21 changes: 15 additions & 6 deletions langchain-core/src/singletons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ class AsyncLocalStorageProvider {
getRunnableConfig() {
const storage = this.getInstance();
// this has the runnable config
// which means that I should also have an instance of a LangChainTracer
// which means that we should also have an instance of a LangChainTracer
// with the run map prepopulated
return storage.getStore()?.extra?.[LC_CHILD_KEY];
}

runWithConfig<T>(config: any, callback: () => T): T {
runWithConfig<T>(
config: any,
callback: () => T,
avoidCreatingRootRunTree?: boolean
): T {
const callbackManager = CallbackManager._configureSync(
config?.callbacks,
undefined,
Expand All @@ -52,10 +56,15 @@ class AsyncLocalStorageProvider {
(handler) => handler?.name === "langchain_tracer"
) as LangChainTracer | undefined;

const runTree =
langChainTracer && parentRunId
? langChainTracer.convertToRunTree(parentRunId)
: new RunTree({ name: "<runnable_lambda>", tracingEnabled: false });
let runTree;
if (langChainTracer && parentRunId) {
runTree = langChainTracer.convertToRunTree(parentRunId);
} else if (!avoidCreatingRootRunTree) {
runTree = new RunTree({
name: "<runnable_lambda>",
tracingEnabled: false,
});
}

if (runTree) {
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
Expand Down
6 changes: 4 additions & 2 deletions langchain-core/src/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export class AsyncGeneratorWithSetup<
} else {
this.firstResult.then((_result) => resolve(undefined as S), reject);
}
}
},
true
);
});
}
Expand All @@ -226,7 +227,8 @@ export class AsyncGeneratorWithSetup<
this.config,
async () => {
return this.generator.next(...args);
}
},
true
);
}

Expand Down

0 comments on commit 609851f

Please sign in to comment.