@@ -57,11 +57,13 @@ export interface AgentState<
5757
5858export type N = typeof START | "agent" | "tools" ;
5959
60- export type StructuredResponseSchemaAndPrompt < StructuredResponseType > = {
61- prompt : string ;
60+ type StructuredResponseSchemaOptions < StructuredResponseType > = {
6261 // eslint-disable-next-line @typescript-eslint/no-explicit-any
6362 schema : InteropZodType < StructuredResponseType > | Record < string , any > ;
64- strict ?:true
63+ prompt ?: string ;
64+
65+ strict ?: boolean ;
66+ [ key : string ] : unknown ;
6567} ;
6668
6769function _convertMessageModifierToPrompt (
@@ -525,14 +527,15 @@ export type CreateReactAgentParams<
525527 */
526528 responseFormat ?:
527529 | InteropZodType < StructuredResponseType >
528- | StructuredResponseSchemaAndPrompt < StructuredResponseType >
529- | { schema : InteropZodType < StructuredResponseType > | Record < string , any > ; strict ?: boolean }
530+ | StructuredResponseSchemaOptions < StructuredResponseType >
530531 // eslint-disable-next-line @typescript-eslint/no-explicit-any
531532 | Record < string , any > ;
533+
532534 /**
533535 * An optional name for the agent.
534536 */
535537 name ?: string ;
538+
536539 /**
537540 * Use to specify how to expose the agent name to the underlying supervisor LLM.
538541
@@ -714,23 +717,18 @@ export function createReactAgent<
714717 const messages = [ ...state . messages ] ;
715718 let modelWithStructuredOutput ;
716719
717- if (
718- typeof responseFormat === "object" &&
719- "prompt" in responseFormat &&
720- "schema" in responseFormat
721- ) {
722- const { prompt, schema, strict } = responseFormat ;
723-
724- modelWithStructuredOutput = ( await _getModel ( llm ) ) . withStructuredOutput (
725- schema ,
726- strict ? { strict :true } : undefined
727- ) ;
728- messages . unshift ( new SystemMessage ( { content : prompt } ) ) ;
729- }
730- else {
731- modelWithStructuredOutput = ( await _getModel ( llm ) ) . withStructuredOutput (
732- responseFormat
733- ) ;
720+ const model = await _getModel ( llm ) ;
721+
722+ if ( typeof responseFormat === "object" && "schema" in responseFormat ) {
723+ const { prompt, schema, ...options } =
724+ responseFormat as StructuredResponseSchemaOptions < StructuredResponseFormat > ;
725+
726+ modelWithStructuredOutput = model . withStructuredOutput ( schema , options ) ;
727+ if ( prompt != null ) {
728+ messages . unshift ( new SystemMessage ( { content : prompt } ) ) ;
729+ }
730+ } else {
731+ modelWithStructuredOutput = model . withStructuredOutput ( responseFormat ) ;
734732 }
735733
736734 const response = await modelWithStructuredOutput . invoke ( messages , config ) ;
@@ -756,7 +754,8 @@ export function createReactAgent<
756754 return { messages : [ response ] } ;
757755 } ;
758756
759- const schema = stateSchema ?? createReactAgentAnnotation < StructuredResponseFormat > ( ) ;
757+ const schema =
758+ stateSchema ?? createReactAgentAnnotation < StructuredResponseFormat > ( ) ;
760759
761760 const workflow = new StateGraph ( schema ) . addNode ( "tools" , toolNode ) ;
762761
0 commit comments