Skip to content
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

anthropic[patch]: Fix Anthropic streaming calls with withStructuredOutput #6339

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions libs/langchain-anthropic/src/output_parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,28 @@ export class AnthropicToolsOutputParser<
}

protected async _validateResult(result: unknown): Promise<T> {
let parsedResult = result;
if (typeof result === "string") {
try {
parsedResult = JSON.parse(result);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
throw new OutputParserException(
`Failed to parse. Text: "${JSON.stringify(
result,
null,
2
)}". Error: ${JSON.stringify(e.message)}`,
result
);
}
} else {
parsedResult = result;
}
if (this.zodSchema === undefined) {
return result as T;
return parsedResult as T;
}
const zodParsedResult = await this.zodSchema.safeParseAsync(result);
const zodParsedResult = await this.zodSchema.safeParseAsync(parsedResult);
if (zodParsedResult.success) {
return zodParsedResult.data;
} else {
Expand All @@ -52,7 +70,7 @@ export class AnthropicToolsOutputParser<
null,
2
)}". Error: ${JSON.stringify(zodParsedResult.error.errors)}`,
JSON.stringify(result, null, 2)
JSON.stringify(parsedResult, null, 2)
);
}
}
Expand Down
23 changes: 22 additions & 1 deletion libs/langchain-anthropic/src/tests/chat_models-tools.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WeatherTool extends StructuredTool {
}

const model = new ChatAnthropic({
modelName: "claude-3-sonnet-20240229",
modelName: "claude-3-haiku-20240307",
temperature: 0,
});

Expand Down Expand Up @@ -440,3 +440,24 @@ test("llm token callbacks can handle tool calls", async () => {
if (!args) return;
expect(args).toEqual(JSON.parse(tokens));
});

test("streaming with structured output", async () => {
const stream = await model
.withStructuredOutput(zodSchema)
.stream("weather in london");
// Currently, streaming yields a single chunk
let finalChunk;
for await (const chunk of stream) {
finalChunk = chunk;
}
expect(typeof finalChunk).toEqual("object");
const stream2 = await model
.withStructuredOutput(zodToJsonSchema(zodSchema))
.stream("weather in london");
// Currently, streaming yields a single chunk
let finalChunk2;
for await (const chunk of stream2) {
finalChunk2 = chunk;
}
expect(typeof finalChunk2).toEqual("object");
});
Loading