@@ -496,7 +496,7 @@ export async function POST(request: NextRequest) {
496496 verbose : 1 ,
497497 logger : ( msg ) => logger . info ( typeof msg === 'string' ? msg : JSON . stringify ( msg ) ) ,
498498 model : {
499- modelName : 'claude-sonnet-4-20250514 ' ,
499+ modelName : 'anthropic/ claude-3-7-sonnet-latest ' ,
500500 apiKey : apiKey ,
501501 } ,
502502 } )
@@ -704,7 +704,14 @@ The system will substitute actual values when these placeholders are used, keepi
704704 ` . trim ( )
705705
706706 const agent = stagehand . agent ( {
707- model : 'anthropic/claude-sonnet-4-20250514' ,
707+ model : {
708+ modelName : 'anthropic/claude-3-7-sonnet-latest' ,
709+ apiKey : apiKey ,
710+ } ,
711+ executionModel : {
712+ modelName : 'anthropic/claude-3-7-sonnet-latest' ,
713+ apiKey : apiKey ,
714+ } ,
708715 systemPrompt : `${ agentInstructions } \n\n${ additionalContext } ` ,
709716 } )
710717
@@ -795,6 +802,9 @@ The system will substitute actual values when these placeholders are used, keepi
795802 } )
796803
797804 let structuredOutput = null
805+ const hasOutputSchema =
806+ outputSchema && typeof outputSchema === 'object' && outputSchema !== null
807+
798808 if ( agentResult . message ) {
799809 try {
800810 let jsonContent = agentResult . message
@@ -807,33 +817,31 @@ The system will substitute actual values when these placeholders are used, keepi
807817 structuredOutput = JSON . parse ( jsonContent )
808818 logger . info ( 'Successfully parsed structured output from agent response' )
809819 } catch ( parseError ) {
810- logger . error ( 'Failed to parse JSON from agent message' , {
811- error : parseError ,
812- message : agentResult . message ,
813- } )
814-
815- if (
816- outputSchema &&
817- typeof outputSchema === 'object' &&
818- outputSchema !== null &&
819- stagehand
820- ) {
821- try {
822- logger . info ( 'Attempting to extract structured data using Stagehand extract' )
823- const schemaObj = getSchemaObject ( outputSchema )
824- const zodSchema = ensureZodObject ( logger , schemaObj )
825-
826- structuredOutput = await stagehand . extract (
827- 'Extract the requested information from this page according to the schema' ,
828- zodSchema
829- )
830-
831- logger . info ( 'Successfully extracted structured data as fallback' , {
832- keys : structuredOutput ? Object . keys ( structuredOutput ) : [ ] ,
833- } )
834- } catch ( extractError ) {
835- logger . error ( 'Fallback extraction also failed' , { error : extractError } )
820+ if ( hasOutputSchema ) {
821+ logger . warn ( 'Failed to parse JSON from agent message, attempting fallback extraction' , {
822+ error : parseError ,
823+ } )
824+
825+ if ( stagehand ) {
826+ try {
827+ logger . info ( 'Attempting to extract structured data using Stagehand extract' )
828+ const schemaObj = getSchemaObject ( outputSchema )
829+ const zodSchema = ensureZodObject ( logger , schemaObj )
830+
831+ structuredOutput = await stagehand . extract (
832+ 'Extract the requested information from this page according to the schema' ,
833+ zodSchema
834+ )
835+
836+ logger . info ( 'Successfully extracted structured data as fallback' , {
837+ keys : structuredOutput ? Object . keys ( structuredOutput ) : [ ] ,
838+ } )
839+ } catch ( extractError ) {
840+ logger . error ( 'Fallback extraction also failed' , { error : extractError } )
841+ }
836842 }
843+ } else {
844+ logger . info ( 'Agent returned plain text response (no schema provided)' )
837845 }
838846 }
839847 }
0 commit comments