Skip to content

Conversation

dcs-soni
Copy link
Contributor

@dcs-soni dcs-soni commented Oct 22, 2025

Description

Handle malformed data URLs in Bedrock, Anthropic, Gemini, and Ollama adapters. Currently, they use unsafe array destructuring when parsing data URLs, causing runtime crashes on malformed input or unexpected results even if the parsed data is not correct base64.

Data URLs can be malformed due to various reasons, like, copy/paste from applications generating invalid URLs, user provided code containing malformed base64 URLs or maybe a network connection during image uploads.

This fix:

  • Prevents runtime crashes on malformed image data.
  • Improves error handling and user experience.
  • Improved response from the providers if the image URLs are malformed.

AI Code Review

  • Team members only: AI review runs automatically when PR is opened or marked ready for review
  • Team members can also trigger a review by commenting @continue-review

Checklist

  • I've read the contributing guide
  • [] The relevant docs, if any, have been updated or created
  • [] The relevant tests, if any, have been updated or created

Screen recording or screenshot

[ When applicable, please include a short screen recording or screenshot - this makes it much easier for us as contributors to review and understand your changes. See this PR as a good example. ]

Tests

[ What tests were added or updated to ensure the changes work as expected? ]


Summary by cubic

Improved image data URL parsing across providers to prevent crashes on malformed inputs. Centralizes parsing logic and adds safer base64 extraction and MIME type handling.

  • Bug Fixes
    • Replaced unsafe split logic with parseDataUrl and extractBase64FromDataUrl helpers.
    • Updated Anthropic, Bedrock, Gemini, Ollama, and their OpenAI adapters to handle invalid/missing image data gracefully.
    • Return clear errors instead of crashing when data URLs are malformed.

@dcs-soni dcs-soni requested a review from a team as a code owner October 22, 2025 07:10
@dcs-soni dcs-soni requested review from RomneyDa and removed request for a team October 22, 2025 07:10
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 22, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 7 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="core/llm/llms/Ollama.ts">

<violation number="1" location="core/llm/llms/Ollama.ts:308">
Calling extractBase64FromDataUrl here will throw on malformed data URLs and bubble up, regressing the previous behavior that tolerated bad input; wrap this in error handling or preserve the non-throwing fallback so malformed URLs don’t crash message conversion.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

if (part.type === "imageUrl" && part.imageUrl) {
const image = part.imageUrl?.url.split(",").at(-1);
const image = part.imageUrl?.url
? extractBase64FromDataUrl(part.imageUrl.url)
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 22, 2025

Choose a reason for hiding this comment

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

Calling extractBase64FromDataUrl here will throw on malformed data URLs and bubble up, regressing the previous behavior that tolerated bad input; wrap this in error handling or preserve the non-throwing fallback so malformed URLs don’t crash message conversion.

Prompt for AI agents
Address the following comment on core/llm/llms/Ollama.ts at line 308:

<comment>Calling extractBase64FromDataUrl here will throw on malformed data URLs and bubble up, regressing the previous behavior that tolerated bad input; wrap this in error handling or preserve the non-throwing fallback so malformed URLs don’t crash message conversion.</comment>

<file context>
@@ -303,7 +304,9 @@ class Ollama extends BaseLLM implements ModelInstaller {
         if (part.type === &quot;imageUrl&quot; &amp;&amp; part.imageUrl) {
-          const image = part.imageUrl?.url.split(&quot;,&quot;).at(-1);
+          const image = part.imageUrl?.url
+            ? extractBase64FromDataUrl(part.imageUrl.url)
+            : undefined;
           if (image) {
</file context>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant