Skip to content

Commit 614d826

Browse files
Merge pull request #652 from simstudioai/fix/resp-format-json-extraction
fix(resp-format): add UI warning for invalid json in response format
2 parents 30538d9 + a0a4b21 commit 614d826

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/components/code.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface CodeProps {
2929
isPreview?: boolean
3030
previewValue?: string | null
3131
disabled?: boolean
32+
onValidationChange?: (isValid: boolean) => void
3233
}
3334

3435
if (typeof document !== 'undefined') {
@@ -60,6 +61,7 @@ export function Code({
6061
isPreview = false,
6162
previewValue,
6263
disabled = false,
64+
onValidationChange,
6365
}: CodeProps) {
6466
// Determine the AI prompt placeholder based on language
6567
const aiPromptPlaceholder = useMemo(() => {
@@ -90,6 +92,27 @@ export function Code({
9092
const showCollapseButton =
9193
(subBlockId === 'responseFormat' || subBlockId === 'code') && code.split('\n').length > 5
9294

95+
const isValidJson = useMemo(() => {
96+
if (subBlockId !== 'responseFormat' || !code.trim()) {
97+
return true
98+
}
99+
try {
100+
JSON.parse(code)
101+
return true
102+
} catch {
103+
return false
104+
}
105+
}, [subBlockId, code])
106+
107+
useEffect(() => {
108+
if (onValidationChange && subBlockId === 'responseFormat') {
109+
const timeoutId = setTimeout(() => {
110+
onValidationChange(isValidJson)
111+
}, 150) // Match debounce time from setStoreValue
112+
return () => clearTimeout(timeoutId)
113+
}
114+
}, [isValidJson, onValidationChange, subBlockId])
115+
93116
const editorRef = useRef<HTMLDivElement>(null)
94117

95118
// Function to toggle collapsed state
@@ -343,9 +366,11 @@ export function Code({
343366

344367
<div
345368
className={cn(
346-
'group relative min-h-[100px] rounded-md border bg-background font-mono text-sm',
347-
isConnecting && 'ring-2 ring-blue-500 ring-offset-2'
369+
'group relative min-h-[100px] rounded-md border bg-background font-mono text-sm transition-colors',
370+
isConnecting && 'ring-2 ring-blue-500 ring-offset-2',
371+
!isValidJson && 'border-2 border-destructive bg-destructive/10'
348372
)}
373+
title={!isValidJson ? 'Invalid JSON' : undefined}
349374
onDragOver={(e) => e.preventDefault()}
350375
onDrop={handleDrop}
351376
>

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/sub-block.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Info } from 'lucide-react'
1+
import { useState } from 'react'
2+
import { AlertTriangle, Info } from 'lucide-react'
23
import { Label } from '@/components/ui/label'
34
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
45
import { getBlock } from '@/blocks/index'
@@ -48,10 +49,16 @@ export function SubBlock({
4849
subBlockValues,
4950
disabled = false,
5051
}: SubBlockProps) {
52+
const [isValidJson, setIsValidJson] = useState(true)
53+
5154
const handleMouseDown = (e: React.MouseEvent) => {
5255
e.stopPropagation()
5356
}
5457

58+
const handleValidationChange = (isValid: boolean) => {
59+
setIsValidJson(isValid)
60+
}
61+
5562
const isFieldRequired = () => {
5663
const blockType = useWorkflowStore.getState().blocks[blockId]?.type
5764
if (!blockType) return false
@@ -169,6 +176,7 @@ export function SubBlock({
169176
isPreview={isPreview}
170177
previewValue={previewValue}
171178
disabled={isDisabled}
179+
onValidationChange={handleValidationChange}
172180
/>
173181
)
174182
case 'switch':
@@ -406,6 +414,16 @@ export function SubBlock({
406414
</TooltipContent>
407415
</Tooltip>
408416
)}
417+
{config.id === 'responseFormat' && !isValidJson && (
418+
<Tooltip>
419+
<TooltipTrigger asChild>
420+
<AlertTriangle className='h-4 w-4 cursor-pointer text-destructive' />
421+
</TooltipTrigger>
422+
<TooltipContent side='top'>
423+
<p>Invalid JSON</p>
424+
</TooltipContent>
425+
</Tooltip>
426+
)}
409427
{config.description && (
410428
<Tooltip>
411429
<TooltipTrigger asChild>

apps/sim/executor/handlers/agent/agent-handler.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ export class AgentBlockHandler implements BlockHandler {
487487
const contentType = response.headers.get('Content-Type')
488488
if (contentType?.includes('text/event-stream')) {
489489
// Handle streaming response
490+
logger.info('Received streaming response')
490491
return this.handleStreamingResponse(response, block)
491492
}
492493

@@ -703,15 +704,31 @@ export class AgentBlockHandler implements BlockHandler {
703704
}
704705

705706
private processStructuredResponse(result: any, responseFormat: any): BlockOutput {
707+
const content = result.content
708+
706709
try {
707-
const parsedContent = JSON.parse(result.content)
710+
const extractedJson = JSON.parse(content.trim())
711+
logger.info('Successfully parsed structured response content')
708712
return {
709-
...parsedContent,
713+
...extractedJson,
710714
...this.createResponseMetadata(result),
711715
}
712716
} catch (error) {
713-
logger.error('Failed to parse response content:', { error })
714-
return this.processStandardResponse(result)
717+
logger.info('JSON parsing failed', {
718+
error: error instanceof Error ? error.message : 'Unknown error',
719+
})
720+
721+
// LLM did not adhere to structured response format
722+
logger.error('LLM did not adhere to structured response format:', {
723+
content: content.substring(0, 200) + (content.length > 200 ? '...' : ''),
724+
responseFormat: responseFormat,
725+
})
726+
727+
const standardResponse = this.processStandardResponse(result)
728+
return Object.assign(standardResponse, {
729+
_responseFormatWarning:
730+
'LLM did not adhere to the specified structured response format. Expected valid JSON but received malformed content. Falling back to standard format.',
731+
})
715732
}
716733
}
717734

0 commit comments

Comments
 (0)