From 47f16cd8c8d143e07c703121521c07853c4532aa Mon Sep 17 00:00:00 2001 From: Emir Karabeg Date: Tue, 11 Mar 2025 15:05:46 -0700 Subject: [PATCH] fix: webhook response format --- app/api/webhooks/trigger/[path]/route.ts | 31 ++++++++++++++++++++---- executor/handlers.ts | 18 ++++++++++++++ providers/openai/index.ts | 15 ++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/app/api/webhooks/trigger/[path]/route.ts b/app/api/webhooks/trigger/[path]/route.ts index 23910fd926..c7f8a36200 100644 --- a/app/api/webhooks/trigger/[path]/route.ts +++ b/app/api/webhooks/trigger/[path]/route.ts @@ -426,14 +426,35 @@ async function processWebhook( // Process the block states to ensure response formats are properly parsed const processedBlockStates = Object.entries(currentBlockStates).reduce( (acc, [blockId, blockState]) => { + // Create a copy of the block state to avoid modifying the original + const processedState = { ...blockState } + // Check if this block has a responseFormat that needs to be parsed - if (blockState.responseFormat && typeof blockState.responseFormat === 'string') { + if (processedState.responseFormat) { try { - const parsedResponseFormat = JSON.parse(blockState.responseFormat) - acc[blockId] = { - ...blockState, - responseFormat: parsedResponseFormat, + // If responseFormat is a string, parse it + if (typeof processedState.responseFormat === 'string') { + processedState.responseFormat = JSON.parse(processedState.responseFormat) } + + // Ensure the responseFormat is properly structured for OpenAI + // This ensures compatibility with the provider's expected format + if ( + processedState.responseFormat && + typeof processedState.responseFormat === 'object' + ) { + // Make sure it has the required properties + if (!processedState.responseFormat.schema && !processedState.responseFormat.name) { + // If it's just a raw schema, wrap it properly + processedState.responseFormat = { + name: 'response_schema', + schema: processedState.responseFormat, + strict: true, + } + } + } + + acc[blockId] = processedState } catch (error) { console.warn(`Failed to parse responseFormat for block ${blockId}:`, error) acc[blockId] = blockState diff --git a/executor/handlers.ts b/executor/handlers.ts index 454fd2ea58..0507c3cc8d 100644 --- a/executor/handlers.ts +++ b/executor/handlers.ts @@ -175,6 +175,24 @@ export class AgentBlockHandler implements BlockHandler { : inputs.responseFormat console.log(`[AgentBlockHandler Debug] Response format after parsing:`, responseFormat) + + // Ensure the responseFormat is properly structured + if (responseFormat && typeof responseFormat === 'object') { + // If it's just a raw schema without the expected wrapper properties, + // wrap it properly for the provider + if (!responseFormat.schema && !responseFormat.name) { + console.log(`[AgentBlockHandler Debug] Wrapping raw schema in proper format`) + responseFormat = { + name: 'response_schema', + schema: responseFormat, + strict: true, + } + } + console.log( + `[AgentBlockHandler Debug] Final response format structure:`, + responseFormat + ) + } } catch (error: any) { console.error(`[AgentBlockHandler Error] Failed to parse response format:`, error) throw new Error(`Invalid response format: ${error.message}`) diff --git a/providers/openai/index.ts b/providers/openai/index.ts index ebd5427d40..c0488525f5 100644 --- a/providers/openai/index.ts +++ b/providers/openai/index.ts @@ -78,6 +78,15 @@ export const openaiProvider: ProviderConfig = { // Add response format for structured output if specified if (request.responseFormat) { + // Log detailed information about the responseFormat + console.log(`[OpenAI Provider Debug] Response format details:`, { + type: typeof request.responseFormat, + isString: typeof request.responseFormat === 'string', + hasSchema: request.responseFormat.schema !== undefined, + hasName: request.responseFormat.name !== undefined, + value: JSON.stringify(request.responseFormat), + }) + // Use OpenAI's JSON schema format payload.response_format = { type: 'json_schema', @@ -87,6 +96,12 @@ export const openaiProvider: ProviderConfig = { strict: request.responseFormat.strict !== false, }, } + + // Log the final response_format being sent to OpenAI + console.log( + `[OpenAI Provider Debug] Final response_format:`, + JSON.stringify(payload.response_format) + ) } // Add tools if provided