Skip to content

fix(ai): resolve missing multipart boundary in toMarkdown transform#2789

Open
Ahmedhajjajofficial wants to merge 1 commit into
cloudflare:mainfrom
Ahmedhajjajofficial:main
Open

fix(ai): resolve missing multipart boundary in toMarkdown transform#2789
Ahmedhajjajofficial wants to merge 1 commit into
cloudflare:mainfrom
Ahmedhajjajofficial:main

Conversation

@Ahmedhajjajofficial

Copy link
Copy Markdown

This PR addresses the critical 400 Bad Request gateway error returned when calling cf.ai.toMarkdown.transform with a raw string parameter. The SDK previously omitted the necessary boundary parameter within the Content-Type: multipart/form-data header 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

  1. Type Discrimination: Introduced an explicit conditional branch validating file instanceof Blob || file instanceof File versus typeof file === 'string'.
  2. Deterministic Payload Building: For string inputs, the boundary is instantiated natively, and the raw payload parts are securely chunked into a cohesive Uint8Array stack.
  3. Dynamic Content-Type Resolution: Employed a standard new Response(finalBody) derivation technique to retrieve the exact runtime-formatted boundary token string seamlessly.

Checklist:

  • My code follows the code style of this project.
  • I have performed a self-review of my own code.
  • My changes generate no new warnings or type declaration errors.
  • I have linked the tracking issue in this description.

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
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.

1 participant