fix(ai): resolve missing multipart boundary in toMarkdown transform#2789
Open
Ahmedhajjajofficial wants to merge 1 commit into
Open
fix(ai): resolve missing multipart boundary in toMarkdown transform#2789Ahmedhajjajofficial wants to merge 1 commit into
Ahmedhajjajofficial wants to merge 1 commit into
Conversation
Fixes a 400 Bad Request error encountered when passing raw strings (such as HTML) directly to the `ai.toMarkdown.transform()` method. The issue occurs because the SDK's internal request wrapper abstracts the payload without preserving or generating the essential multipart/form-data boundary required by the Cloudflare Gateway. This patch introduces robust runtime type-checking and automated fallback handling within the SDK layer to manually construct the multipart payload chunks for raw strings using TextEncoder and dynamically extract the system-generated content-type via a synthetic Response abstraction. Closes cloudflare#2779
mattikasritanjanta55-afk
referenced
this pull request
Jun 30, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR addresses the critical
400 Bad Requestgateway error returned when callingcf.ai.toMarkdown.transformwith a raw string parameter. The SDK previously omitted the necessaryboundaryparameter within theContent-Type: multipart/form-dataheader when handling non-pre-formed payloads.Motivation and Context
When developers pass raw HTML strings directly into the API for conversion, the request fails because the Cloudflare edge gateway cannot parse the un-bounded multipart body. While a manual user-land workaround exists, implementing automated type detection and safe fallback parsing directly inside the SDK significantly enhances the Developer Experience (DX).
Ref: #2779
Technical Approach
file instanceof Blob || file instanceof Fileversustypeof file === 'string'.stringinputs, the boundary is instantiated natively, and the raw payload parts are securely chunked into a cohesiveUint8Arraystack.new Response(finalBody)derivation technique to retrieve the exact runtime-formatted boundary token string seamlessly.Checklist: