Skip to content

Commit 86adfb7

Browse files
committed
fix: respect strict option in responseFormat inside createReactAgent
1 parent d53c891 commit 86adfb7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

libs/langgraph/src/prebuilt/react_agent_executor.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type StructuredResponseSchemaAndPrompt<StructuredResponseType> = {
6161
prompt: string;
6262
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6363
schema: InteropZodType<StructuredResponseType> | Record<string, any>;
64+
strict?:true
6465
};
6566

6667
function _convertMessageModifierToPrompt(
@@ -478,6 +479,7 @@ export type CreateReactAgentParams<
478479
* @deprecated Use prompt instead.
479480
*/
480481
stateModifier?: StateModifier;
482+
481483
/**
482484
* An optional prompt for the LLM. This takes full graph state BEFORE the LLM is called and prepares the input to LLM.
483485
*
@@ -524,6 +526,7 @@ export type CreateReactAgentParams<
524526
responseFormat?:
525527
| InteropZodType<StructuredResponseType>
526528
| StructuredResponseSchemaAndPrompt<StructuredResponseType>
529+
| { schema: InteropZodType<StructuredResponseType> | Record<string, any>; strict?: boolean }
527530
// eslint-disable-next-line @typescript-eslint/no-explicit-any
528531
| Record<string, any>;
529532
/**
@@ -714,14 +717,17 @@ export function createReactAgent<
714717
if (
715718
typeof responseFormat === "object" &&
716719
"prompt" in responseFormat &&
717-
"schema" in responseFormat
720+
"schema" in responseFormat
718721
) {
719-
const { prompt, schema } = responseFormat;
722+
const { prompt, schema,strict } = responseFormat;
723+
720724
modelWithStructuredOutput = (await _getModel(llm)).withStructuredOutput(
721-
schema
725+
schema,
726+
strict ? {strict:true} : undefined
722727
);
723728
messages.unshift(new SystemMessage({ content: prompt }));
724-
} else {
729+
}
730+
else {
725731
modelWithStructuredOutput = (await _getModel(llm)).withStructuredOutput(
726732
responseFormat
727733
);
@@ -750,8 +756,7 @@ export function createReactAgent<
750756
return { messages: [response] };
751757
};
752758

753-
const schema =
754-
stateSchema ?? createReactAgentAnnotation<StructuredResponseFormat>();
759+
const schema =stateSchema ?? createReactAgentAnnotation<StructuredResponseFormat>();
755760

756761
const workflow = new StateGraph(schema).addNode("tools", toolNode);
757762

0 commit comments

Comments
 (0)