Skip to content

Fix #4498: Strip File objects from messages when multimodal is unsupported#4499

Open
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1771258257-fix-file-not-json-serializable
Open

Fix #4498: Strip File objects from messages when multimodal is unsupported#4499
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1771258257-fix-file-not-json-serializable

Conversation

@devin-ai-integration
Copy link
Contributor

Fix #4498: Strip File objects from messages when multimodal is unsupported

Summary

When input_files containing File objects are passed to a model that doesn't support multimodal (e.g., gemma3 via Ollama), _process_message_files returned early without removing the files key from message dicts. These non-serializable File objects then leaked into the API payload, causing TypeError: Object of type File is not JSON serializable from httpx.

The fix ensures the files key is always stripped from every message, regardless of multimodal support. A warning is logged when files are dropped. The same fix is applied to three code paths:

  • BaseLLM._process_message_files (sync — native providers)
  • LLM._process_message_files (sync — LiteLLM wrapper)
  • LLM._aprocess_message_files (async — LiteLLM wrapper)

8 regression tests added covering BaseLLM, LiteLLM sync/async, OpenAI native, JSON serializability, multiple messages, and warning logging.

Review & Testing Checklist for Human

  • Verify the multimodal-supported path still works: pass input_files with an image to a vision model (e.g., gpt-4o) and confirm files are correctly converted to content blocks (not dropped). The tests only cover the unsupported path directly.
  • Check that the per-message warning log ("dropped from the request") won't be excessively noisy for users who pass many messages with files to non-multimodal models. Consider if it should log once instead.
  • Note that provider/api variable lookups moved inside the loop body — functionally equivalent but only evaluated for messages that actually have files.

Notes

Fixes #4498. When  returns False,
 previously returned early without removing
the  key from messages. This left non-serializable File objects
in the payload, causing TypeError during JSON serialization by httpx.

Now the  key is always stripped from every message, with a
warning logged when files are dropped because the model lacks
multimodal support or crewai-files is not installed.

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

[BUG] TypeError: Object of type File is not JSON serializable when using input_files with gemma3

0 participants

Comments