Skip to content

Instantly share code, notes, and snippets.

@richardscarrott
Created December 22, 2025 12:56
Show Gist options
  • Select an option

  • Save richardscarrott/b05f187b13cc7ef3429970aecc6b844e to your computer and use it in GitHub Desktop.

Select an option

Save richardscarrott/b05f187b13cc7ef3429970aecc6b844e to your computer and use it in GitHub Desktop.
Generators wrapped with LangSmith traceable skip finally/dispose when iteration stops early
import { traceable } from "langsmith/traceable";
const createDisposable = function (name: string) {
return {
name,
async [Symbol.asyncDispose]() {
console.log(`${name}: disposing`);
},
};
};
async function* generator(name: string) {
await using disposable = createDisposable(name);
try {
console.log(`${name}: In try block`);
yield `${name}: yield 1`;
yield `${name}: yield 2`;
} catch (ex) {
console.log(`${name}: caught exception`);
} finally {
console.log(`${name}: In finally block`);
}
}
async function demo(
name: string,
it: (name: string) => AsyncGenerator<string>,
) {
console.log(`${name}: demo started`);
for await (const v of it(name)) {
console.log(v);
break; // stop early
}
console.log(`${name}: demo complete`);
}
demo("without traceable", generator);
// without traceable: demo started
// without traceable: In try block
// without traceable: yield 1
// without traceable: In finally block
// without traceable: disposing
// without traceable: demo complete
const traceableGenerator = traceable(generator, { name: "traceable_test" });
demo("with traceable", traceableGenerator);
// EXPECT:
// without traceable: demo started
// without traceable: In try block
// without traceable: yield 1
// without traceable: In finally block
// without traceable: disposing
// without traceable: demo complete
// ACTUAL:
// with traceable: demo started
// with traceable: In try block
// with traceable: yield 1
// with traceable: demo complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment