-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix: safely render structured output objects to prevent React error #31 #3483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxwellCalkin
wants to merge
2
commits into
simstudioai:main
Choose a base branch
from
MaxwellCalkin:fix/react-error-31-object-rendering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { safeRenderValue } from '@/lib/core/utils/safe-render' | ||
|
|
||
| describe('safeRenderValue', () => { | ||
| it('returns empty string for null', () => { | ||
| expect(safeRenderValue(null)).toBe('') | ||
| }) | ||
|
|
||
| it('returns empty string for undefined', () => { | ||
| expect(safeRenderValue(undefined)).toBe('') | ||
| }) | ||
|
|
||
| it('returns string values unchanged', () => { | ||
| expect(safeRenderValue('hello world')).toBe('hello world') | ||
| }) | ||
|
|
||
| it('returns empty string for empty string input', () => { | ||
| expect(safeRenderValue('')).toBe('') | ||
| }) | ||
|
|
||
| it('converts numbers to string', () => { | ||
| expect(safeRenderValue(42)).toBe('42') | ||
| expect(safeRenderValue(0)).toBe('0') | ||
| expect(safeRenderValue(-1.5)).toBe('-1.5') | ||
| }) | ||
|
|
||
| it('converts booleans to string', () => { | ||
| expect(safeRenderValue(true)).toBe('true') | ||
| expect(safeRenderValue(false)).toBe('false') | ||
| }) | ||
|
|
||
| it('extracts text from {text, type} content block objects', () => { | ||
| expect(safeRenderValue({ text: 'Hello from AI', type: 'text' })).toBe('Hello from AI') | ||
| }) | ||
|
|
||
| it('extracts text from {text} objects without type', () => { | ||
| expect(safeRenderValue({ text: 'Some text' })).toBe('Some text') | ||
| }) | ||
|
|
||
| it('joins text from arrays of content blocks', () => { | ||
| const contentArray = [ | ||
| { text: 'Hello ', type: 'text' }, | ||
| { text: 'world', type: 'text' }, | ||
| ] | ||
| expect(safeRenderValue(contentArray)).toBe('Hello world') | ||
| }) | ||
|
|
||
| it('handles arrays with mixed content types', () => { | ||
| const mixedArray = [ | ||
| { text: 'Text part', type: 'text' }, | ||
| { type: 'tool_use', id: '123', name: 'search' }, | ||
| ] | ||
| const result = safeRenderValue(mixedArray) | ||
| expect(result).toContain('Text part') | ||
| expect(result).toContain('tool_use') | ||
| }) | ||
|
|
||
| it('handles string arrays', () => { | ||
| expect(safeRenderValue(['hello', 'world'])).toBe('helloworld') | ||
| }) | ||
|
|
||
| it('JSON-stringifies plain objects without text property', () => { | ||
| const obj = { key: 'value', nested: { a: 1 } } | ||
| expect(safeRenderValue(obj)).toBe(JSON.stringify(obj, null, 2)) | ||
| }) | ||
|
|
||
| it('JSON-stringifies empty objects', () => { | ||
| expect(safeRenderValue({})).toBe('{}') | ||
| }) | ||
|
|
||
| it('handles empty arrays', () => { | ||
| expect(safeRenderValue([])).toBe('[]') | ||
| }) | ||
|
|
||
| it('does not extract text when text property is not a string', () => { | ||
| const obj = { text: 42, type: 'number' } | ||
| expect(safeRenderValue(obj)).toBe(JSON.stringify(obj, null, 2)) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Safely converts a value to a string suitable for rendering in JSX. | ||
| * | ||
| * Prevents React error #31 ("Objects are not valid as a React child") by | ||
| * ensuring that structured objects (e.g. `{ text, type }` content blocks | ||
| * returned by LLM providers) are converted to a displayable string instead | ||
| * of being passed directly as React children. | ||
| * | ||
| * @param value - The value to convert. Can be a string, number, boolean, | ||
| * null, undefined, array, or object. | ||
| * @returns A string representation safe for rendering in JSX. | ||
| */ | ||
| export function safeRenderValue(value: unknown): string { | ||
| if (value === null || value === undefined) { | ||
| return '' | ||
| } | ||
|
|
||
| if (typeof value === 'string') { | ||
| return value | ||
| } | ||
|
|
||
| if (typeof value === 'number' || typeof value === 'boolean') { | ||
| return String(value) | ||
| } | ||
|
|
||
| if (typeof value === 'object') { | ||
| // Handle content block objects like { text, type } from LLM providers | ||
| // by extracting the text property when available | ||
| if ( | ||
| !Array.isArray(value) && | ||
| 'text' in value && | ||
| typeof (value as Record<string, unknown>).text === 'string' | ||
| ) { | ||
| return (value as Record<string, unknown>).text as string | ||
| } | ||
|
|
||
| // Handle arrays of content blocks (e.g. Anthropic's content array) | ||
| if (Array.isArray(value)) { | ||
| const textParts = value | ||
| .map((item) => { | ||
| if (typeof item === 'string') return item | ||
| if ( | ||
| item && | ||
| typeof item === 'object' && | ||
| 'text' in item && | ||
| typeof item.text === 'string' | ||
| ) { | ||
| return item.text | ||
| } | ||
| return JSON.stringify(item) | ||
| }) | ||
| .filter(Boolean) | ||
|
|
||
| if (textParts.length > 0) { | ||
| return textParts.join('') | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| return JSON.stringify(value, null, 2) | ||
| } catch { | ||
| return String(value) | ||
| } | ||
| } | ||
|
|
||
| return String(value) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistency:
isJsonObjectbranch usesJSON.stringifyinstead ofsafeRenderValueWhen
isJsonObjectistrue(i.e., when a structured content block like{ text: "Hello", type: "text" }is returned),cleanTextContentis computed viaJSON.stringify, which displays the raw JSON structure in the<pre>block rather than extracting the.textfield.This is inconsistent with
chat-message.tsxin the workspace surface, which usessafeRenderValueunconditionally and correctly extracts the.textfield. As a result, the same content block displays differently across the two chat surfaces: raw JSON in public chat vs. extracted text in workspace chat.Consider using
safeRenderValuefor both branches:If displaying formatted JSON for non-content-block objects is intentional, the
isJsonObjectflag can still control the<pre>vs<span>rendering decision without affecting text extraction.