-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(#5626 #5832): handle multiple content chunks & images better #5839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(#5626 #5832): handle multiple content chunks & images better #5839
Conversation
There was a problem hiding this 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_arrayandtext_arrayfor accumulating message content - Enables image support in Anthropic format by calling
convert_imageinstead 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 |
| 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()); |
Copilot
AI
Nov 21, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
michaelneale
left a comment
There was a problem hiding this 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
b83cdd7 to
69ff62f
Compare
Co-authored-by: Pradeepta Dash <[email protected]>
69ff62f to
978aa16
Compare
There was a problem hiding this 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
| if !content_array.is_empty() { | ||
| converted["content"] = json!(content_array); | ||
| } else if !text_array.is_empty() { | ||
| converted["content"] = json!(text_array.join("\n")); |
Copilot
AI
Nov 24, 2025
There was a problem hiding this comment.
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.
| 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); |
|
@michaelneale Good call. I will think how to get tests going, but merging this for now to have the bugfix in. |
* 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)
…etter (block#5839) Co-authored-by: Pradeepta Dash <[email protected]>
…etter (block#5839) Co-authored-by: Pradeepta Dash <[email protected]> Signed-off-by: Sai Karthik <[email protected]>
…etter (block#5839) Co-authored-by: Pradeepta Dash <[email protected]> Signed-off-by: Blair Allan <[email protected]>
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