Skip to content

Commit

Permalink
Fix Anthropic streaming calls with withStructuredOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Aug 2, 2024
1 parent 9578e55 commit ee4d2c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
23 changes: 20 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,27 @@ export class AnthropicToolsOutputParser<
}

protected async _validateResult(result: unknown): Promise<T> {
let parsedResult = result;
if (typeof result === "string") {
try {
parsedResult = JSON.parse(result);
} catch (e: any) {

Check failure on line 46 in libs/langchain-anthropic/src/output_parsers.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
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 +69,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.only("streaming with structured output", async () => {

Check failure on line 444 in libs/langchain-anthropic/src/tests/chat_models-tools.int.test.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected focused test
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");
});

0 comments on commit ee4d2c2

Please sign in to comment.