Skip to content

Conversation

@alexhancock
Copy link
Collaborator

Combined fix for #5832 and #5626

See original description of the fix in #5838 and then I think this combined coauthored commit should fix both issues

cc @Pdash-exceeds @michaelneale

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issues with handling multiple text content chunks and images in message formatting for both OpenAI and Anthropic providers. It refactors the message accumulation logic to properly collect text and image content separately, then combine them appropriately based on whether images are present.

Key changes:

  • Refactors OpenAI format to use separate content_array and text_array for accumulating message content
  • Enables image support in Anthropic format by calling convert_image instead of skipping images
  • Adds test coverage for multiple text blocks in messages

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/goose/src/providers/formats/openai.rs Refactors message formatting to accumulate text and image content separately, then combines them based on whether images are present; adds test for multiple text blocks
crates/goose/src/providers/formats/anthropic.rs Enables image content support by importing and using convert_image utility

Comment on lines 72 to +81
if let Some(image_path) = detect_image_path(&text.text) {
// Try to load and convert the image
if let Ok(image) = load_image_file(image_path) {
converted["content"] = json!([
{"type": "text", "text": text.text},
convert_image(&image, image_format)
]);
content_array.push(json!({"type": "text", "text": text.text}));
content_array.push(convert_image(&image, image_format));
} else {
// If image loading fails, just use the text
converted["content"] = json!(text.text);
text_array.push(text.text.clone());
}
} else {
converted["content"] = json!(text.text);
text_array.push(text.text.clone());
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

When an image path is detected and loaded successfully, the text is added to content_array but subsequent non-image text goes to text_array. This will cause text_array content to be lost since line 243-246 only uses content_array when it's non-empty. All text should go to content_array when any images are present.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this legit feedback @alexhancock ? it sounds convincing

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it may be slop as further down is consolidated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah below I do

        if !content_array.is_empty() {
            converted["content"] = json!(content_array);
        } else if !text_array.is_empty() {
            converted["content"] = json!(text_array.join("\n"));
        }

which seems right

Copy link
Collaborator

@michaelneale michaelneale left a comment

Choose a reason for hiding this comment

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

do we have time to add this to live test coverage? (what would prompt be to show images) - would be happy if we could do that for these 2 providers @alexhancock

@alexhancock alexhancock force-pushed the Pdash-exceeds+alexhancock/multi-content-chunks branch from b83cdd7 to 69ff62f Compare November 24, 2025 15:07
Copilot AI review requested due to automatic review settings November 24, 2025 15:35
@alexhancock alexhancock force-pushed the Pdash-exceeds+alexhancock/multi-content-chunks branch from 69ff62f to 978aa16 Compare November 24, 2025 15:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • ui/desktop/package-lock.json: Language not supported

Comment on lines +243 to +246
if !content_array.is_empty() {
converted["content"] = json!(content_array);
} else if !text_array.is_empty() {
converted["content"] = json!(text_array.join("\n"));
Copy link

Copilot AI Nov 24, 2025

Choose a reason for hiding this comment

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

Text content is lost when both content_array and text_array have items. If a message has text, then an image, then more text, the text in text_array is discarded because only content_array is used. Fix: merge text_array into content_array before checking, e.g., if !text_array.is_empty() { content_array.push(json!({"type": "text", "text": text_array.join("\n")})); } before line 243.

Suggested change
if !content_array.is_empty() {
converted["content"] = json!(content_array);
} else if !text_array.is_empty() {
converted["content"] = json!(text_array.join("\n"));
// Merge text_array into content_array if text exists
if !text_array.is_empty() {
content_array.push(json!({"type": "text", "text": text_array.join("\n")}));
}
if !content_array.is_empty() {
converted["content"] = json!(content_array);

Copilot uses AI. Check for mistakes.
@alexhancock
Copy link
Collaborator Author

@michaelneale Good call. I will think how to get tests going, but merging this for now to have the bugfix in.

@alexhancock alexhancock merged commit c82a0dd into main Nov 24, 2025
23 checks passed
@alexhancock alexhancock deleted the Pdash-exceeds+alexhancock/multi-content-chunks branch November 24, 2025 15:52
michaelneale added a commit that referenced this pull request Nov 25, 2025
* main:
  docs: add DataHub MCP server extension documentation (#5769)
  docs: lowercase goose in remaining topics (#5861)
  docs: lowercase goose in getting-started and guides topics (#5857)
  Fix multi tool calling (#5855)
  fix(#5626 #5832): handle multiple content chunks & images better (#5839)
  chore: some old code hanging around, and mention configure cli (#5822)
  feat : add support for math / science symbology via katex (#5773)
  feat : add ability to see error message in toast (#5851)
kskarthik pushed a commit to kskarthik/goose that referenced this pull request Nov 25, 2025
kskarthik pushed a commit to kskarthik/goose that referenced this pull request Nov 26, 2025
BlairAllan pushed a commit to BlairAllan/goose that referenced this pull request Nov 29, 2025
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.

3 participants