Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,49 @@ function useBamlAction<FunctionName extends FunctionNames>(
streamData: undefined,
})

// Recursively serialize any BAML media types nested inside args (e.g., inside dynamic classes)
function serializeBamlMediaDeep(value: unknown): unknown {
if (value && typeof value === 'object') {
const ctorName = (value as any).constructor?.name;
if (
ctorName === 'BamlImage' ||
ctorName === 'BamlAudio' ||
ctorName === 'BamlPdf' ||
ctorName === 'BamlVideo'
) {
try {
return (value as any).toJSON();
} catch {
return value;
}
}

if (Array.isArray(value)) {
return value.map(serializeBamlMediaDeep);
}

// Preserve common built-ins
if (value instanceof Date) return value;
if (typeof File !== 'undefined' && value instanceof File) return value;
if (typeof Blob !== 'undefined' && value instanceof Blob) return value;

const result: Record<string, unknown> = {};
for (const key of Object.keys(value as Record<string, unknown>)) {
result[key] = serializeBamlMediaDeep((value as any)[key]);
}
return result;
}
return value;
}

const mutate = useCallback(
async (...input: Parameters<ServerAction>) => {
dispatch({ type: 'START_REQUEST' })
try {
let response: Awaited<ReturnType<ServerAction>>
startTransition(async () => {
// Transform any BamlImage or BamlAudio inputs to their JSON representation
const transformedInput = input.map(arg => {
// Check if the argument is an instance of BamlImage or BamlAudio
// We check the constructor name since the actual classes might be proxied in browser environments
if (arg && typeof arg === 'object' &&
(arg.constructor.name === 'BamlImage' || arg.constructor.name === 'BamlAudio')) {
return arg.toJSON();
}
return arg;
});
// Transform any BAML media inputs (even when nested) to their JSON representation
const transformedInput = input.map(serializeBamlMediaDeep)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Circular Reference Handling in Serialization

The serializeBamlMediaDeep function recursively traverses objects without tracking visited objects. This can lead to infinite recursion and a stack overflow if the input contains circular references.

Additional Locations (1)

Fix in Cursor Fix in Web


response = await action(...transformedInput)

Expand Down
1 change: 0 additions & 1 deletion integ-tests/go/baml_client/functions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion integ-tests/go/baml_client/functions_parse.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion integ-tests/go/baml_client/functions_parse_stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion integ-tests/go/baml_client/functions_stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions integ-tests/go/baml_client/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integ-tests/go/baml_client/stream_types/classes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions integ-tests/go/baml_client/stream_types/type_aliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions integ-tests/go/baml_client/stream_types/unions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions integ-tests/go/baml_client/stream_types/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions integ-tests/go/baml_client/type_builder/type_builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions integ-tests/go/baml_client/type_map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integ-tests/go/baml_client/types/classes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions integ-tests/go/baml_client/types/type_aliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 37 additions & 10 deletions integ-tests/react/baml_client/react/hooks.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading