Skip to content

Comments

fix: strip File objects from messages for non-multimodal LLMs#4536

Open
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA:fix/file-json-serialization
Open

fix: strip File objects from messages for non-multimodal LLMs#4536
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA:fix/file-json-serialization

Conversation

@hztBUAA
Copy link

@hztBUAA hztBUAA commented Feb 20, 2026

Summary

  • Fixes TypeError: Object of type File is not JSON serializable when using input_files with LLMs that don't support multimodal (e.g., Ollama/Gemma 3)
  • Root cause: BaseLLM._process_message_files() returned early for non-multimodal LLMs without removing the files key from messages, leaving non-serializable File objects in the message dicts that get passed to json.dumps() by the httpx client
  • Fix: always strip the files key from all messages when multimodal is not supported, before returning

Test plan

  • Added 5 unit tests in TestProcessMessageFiles class covering:
    • Non-multimodal LLM strips files key from messages
    • Messages are JSON-serializable after processing (reproduces the exact TypeError from the issue)
    • Messages without files key are unaffected
    • Multimodal LLM also strips files after converting to content blocks
    • Multimodal LLM properly converts files to provider content blocks

Fixes #4498

When an LLM does not support multimodal inputs (e.g. Ollama/Gemma 3),
_process_message_files() returned early without removing the `files`
key from messages. The File objects (not JSON-serializable) then
flowed through to the OpenAI SDK, causing:
  TypeError: Object of type File is not JSON serializable

Now the `files` key is always stripped from messages, regardless of
whether the LLM supports multimodal. This ensures messages are always
JSON-serializable when passed to any provider's API client.

Fixes crewAIInc#4498
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

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

1 participant