Skip to content

Conversation

aaronvg
Copy link
Contributor

@aaronvg aaronvg commented Sep 24, 2025

Pull Request Template

Thanks for taking the time to fill out this pull request!

Issue Reference

Please link to any related issues

  • This PR fixes/closes # (No specific issue number provided, addresses the problem described in the task)

Changes

Please describe the changes proposed in this pull request

This PR introduces a deep serialization mechanism for BAML media types (Image, Audio, Pdf, Video) within React hooks. Previously, only top-level BAML media instances were serialized to their JSON representation before being sent to server actions. This change adds a serializeBamlMediaDeep helper function that recursively traverses input objects and arrays, ensuring that any nested BAML media instances are correctly converted to JSON.

The mutate function in the useBamlAction hook now uses this deep serialization, preventing nested media from being stripped by Next.js server action serialization.

Testing

Please describe how you tested these changes

  • Unit tests added/updated
  • Manual testing performed (Logic verified through code inspection and understanding of Next.js serialization behavior)
  • Tested in [environment]

Screenshots

If applicable, add screenshots to help explain your changes

[Add screenshots here...]

PR Checklist

Please ensure you've completed these items

  • I have read and followed the contributing guidelines
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

Additional Notes

Add any other context about the PR here

Root Cause: When a BamlImage (or other BAML media) was nested inside a dynamic class or array as an input parameter to a BAML function called via a React hook, it was not being properly serialized. The useBamlAction hook only performed a shallow transformation of top-level arguments. Consequently, Next.js server action serialization would strip the private fields of the BamlImage object, effectively turning it into an empty object {} before it reached the server, causing it to "disappear."

Fix: The serializeBamlMediaDeep helper ensures that all BAML media instances, regardless of their nesting level within objects or arrays, are converted to their JSON representation ({ url } or { base64, media_type }) before the server action call. This allows the server-side argument coercer to correctly recognize and process the media.

The Go file changes are unrelated cleanups (removal of unused imports) and do not directly pertain to the BAML media serialization fix.


Open in Cursor Open in Web

Copy link

cursor bot commented Sep 24, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@aaronvg aaronvg temporarily deployed to boundary-tools-dev September 24, 2025 20:50 — with GitHub Actions Inactive
@aaronvg aaronvg temporarily deployed to boundary-tools-dev September 24, 2025 20:50 — with GitHub Actions Inactive
@aaronvg aaronvg temporarily deployed to boundary-tools-dev September 24, 2025 20:50 — with GitHub Actions Inactive
Copy link

vercel bot commented Sep 24, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
promptfiddle Skipped Skipped Sep 24, 2025 9:56pm

Copy link

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

Copy link

codecov bot commented Sep 24, 2025

Codecov Report

❌ Patch coverage is 83.69565% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
engine/baml-lib/jinja-runtime/src/lib.rs 83.69% 15 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link

This commit refactors the media parsing logic in the jinja runtime to be more robust and tolerant of surrounding characters. It also updates the react hooks generator to only serialize BamlImage and BamlAudio types, as other media types are not supported by the current implementation.

Co-authored-by: aaron <[email protected]>
@cursor cursor bot temporarily deployed to boundary-tools-dev September 24, 2025 21:56 Inactive
@cursor cursor bot temporarily deployed to boundary-tools-dev September 24, 2025 21:56 Inactive
@cursor cursor bot temporarily deployed to boundary-tools-dev September 24, 2025 21:56 Inactive
Copy link

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants