Skip to content

fix(llms): inline text files for text-only models#5834

Open
pragnyanramtha wants to merge 4 commits into
crewAIInc:mainfrom
pragnyanramtha:pragnyan/inline-text-files-text-only-models-5137
Open

fix(llms): inline text files for text-only models#5834
pragnyanramtha wants to merge 4 commits into
crewAIInc:mainfrom
pragnyanramtha:pragnyan/inline-text-files-text-only-models-5137

Conversation

@pragnyanramtha
Copy link
Copy Markdown

@pragnyanramtha pragnyanramtha commented May 16, 2026

Summary

  • inline text-like input_files into the prompt when the selected model is not multimodal
  • keep the existing multimodal error for non-text attachments on text-only models
  • add regression coverage for text files on openai/gpt-3.5-turbo

Fixes #5137.

Context

input_files currently trips the non-multimodal guard before file content is considered. Text files do not require vision support, so users hit the multimodal error even though the file can be represented as ordinary prompt text.

This patch keeps image/PDF/non-text attachments on the existing vision-model path, while converting text-like files into appended prompt text for text-only models.

Validation

  • uv run pytest lib/crewai/tests/llms/test_multimodal.py::TestOpenAIMultimodal::test_non_multimodal_model_accepts_text_files lib/crewai/tests/llms/test_multimodal.py::TestOpenAIMultimodal::test_does_not_support_gpt35 -q -> 2 passed
  • uv run ruff check lib/crewai/src/crewai/llms/base_llm.py lib/crewai/tests/llms/test_multimodal.py -> passed
  • uv run ruff format --check lib/crewai/src/crewai/llms/base_llm.py lib/crewai/tests/llms/test_multimodal.py -> passed
  • uv run mypy lib/crewai/src/crewai/llms/base_llm.py -> passed
  • git diff --check -> passed

I also attempted the full lib/crewai/tests/llms/test_multimodal.py file in this checkout, but optional LiteLLM / Google GenAI provider extras are not installed here; the resulting failures were missing-provider setup failures rather than this text-file path.

Summary by CodeRabbit

  • New Features

    • Non-multimodal language models can now accept text file attachments; file contents are automatically embedded into the message content with an "Attached file: " block.
    • Image or other non-text attachments remain rejected for non-multimodal models.
  • Tests

    • Added tests verifying text-file inlining for non-multimodal models and that image inputs are still rejected.

Review Change Stack

Copilot AI review requested due to automatic review settings May 16, 2026 16:46
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cac2f163-d7bf-4593-9c31-406a530bf7b7

📥 Commits

Reviewing files that changed from the base of the PR and between fe782e9 and 4699ad2.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/llms/base_llm.py
  • lib/crewai/tests/llms/test_multimodal.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/crewai/src/crewai/llms/base_llm.py

📝 Walkthrough

Walkthrough

BaseLLM adds support for inlining text-file attachments for non-multimodal models. A MIME-type allowlist and _is_text_file identify text files; _process_text_files_for_text_only_model reads files via read_text(), appends "Attached file: {name}" sections to message content, and removes the files field. Tests cover accepted text and rejected images.

Changes

Text file support for non-multimodal models

Layer / File(s) Summary
MIME allowlist and callsite update
lib/crewai/src/crewai/llms/base_llm.py
Adds a module-level allowlist of text MIME types and updates _process_message_files to call the inlining helper for non-multimodal models instead of always raising when files are present.
Text-file validation and inlining implementation
lib/crewai/src/crewai/llms/base_llm.py
Adds BaseLLM._is_text_file static check and _process_text_files_for_text_only_model which validates files dict and content type, reads attachments with read_text(), appends Attached file: {name} blocks to message content, and removes the files field.
Test verification for text file inlining
lib/crewai/tests/llms/test_multimodal.py
Adds tests asserting non-multimodal models (e.g., openai/gpt-3.5-turbo) inline TextFile contents into user content, accept text-like MIME parameters, and continue to reject image/binary inputs; adjusts an assertion line in an existing empty-files test.

"🐰 I nibbled through bytes and found the text,
Poked it into prompts where it now rests.
Attached and named, each file takes its seat,
For models that read words — not pictures — to meet. 🥕📜"

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: enabling text file inlining for text-only models, which directly addresses the core fix.
Linked Issues check ✅ Passed The pull request fulfills all coding requirements from issue #5137: inlines text files for non-multimodal models, preserves multimodal validation for non-text files, adds test coverage, and maintains backward compatibility.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the issue: modifying base_llm.py to support text file inlining and adding regression tests in test_multimodal.py with no extraneous modifications.
Docstring Coverage ✅ Passed Docstring coverage is 90.91% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
lib/crewai/tests/llms/test_multimodal.py (1)

176-199: ⚡ Quick win

Add the complementary non-text rejection regression in this same block.

This new test covers success for TextFile, but the contract also depends on non-text inputs still failing on text-only models. Adding an adjacent pytest.raises(ValueError) case (e.g., ImageFile) will lock in both sides of the behavior.

🧪 Suggested test addition
 class TestOpenAIMultimodal:
@@
     def test_non_multimodal_model_accepts_text_files(self) -> None:
         """Test text files are inlined for models without multimodal support."""
@@
         assert result == [
             {
                 "role": "user",
                 "content": (
                     "Summarize the attached file.\n\n"
                     "Attached file: readme\n"
                     "hello from a text file"
                 ),
             }
         ]
+
+    def test_non_multimodal_model_rejects_non_text_files(self) -> None:
+        """Test non-text files still fail for models without multimodal support."""
+        llm = LLM(model="openai/gpt-3.5-turbo")
+        messages = [
+            {
+                "role": "user",
+                "content": "Describe this image.",
+                "files": {"image": ImageFile(source=MINIMAL_PNG)},
+            }
+        ]
+
+        with pytest.raises(ValueError, match="does not support multimodal input"):
+            llm._format_messages(messages)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/tests/llms/test_multimodal.py` around lines 176 - 199, Extend the
existing test_non_multimodal_model_accepts_text_files by adding a complementary
negative case that asserts non-text files are rejected for text-only models:
call llm._format_messages (same LLM(model="openai/gpt-3.5-turbo")) with a
message that contains an ImageFile (or other non-text file type) in the files
dict and wrap the call in pytest.raises(ValueError) to ensure a ValueError is
thrown; reference the existing test_non_multimodal_model_accepts_text_files, the
LLM._format_messages method, TextFile and ImageFile symbols, and
pytest.raises(ValueError) so both acceptance and rejection behaviors are locked
in.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/crewai/src/crewai/llms/base_llm.py`:
- Around line 825-833: Normalize the MIME type string before matching by
stripping any parameters and lowercasing it: after fetching content_type =
getattr(file_input, "content_type", "") (and confirming it's a str), replace
content_type with content_type.split(";", 1)[0].strip().lower() and then perform
the existing checks (callable(getattr(file_input, "read_text", None)) and
(content_type.startswith("text/") or content_type in _TEXT_FILE_CONTENT_TYPES));
reference symbols: content_type, file_input, read_text, and
_TEXT_FILE_CONTENT_TYPES.

---

Nitpick comments:
In `@lib/crewai/tests/llms/test_multimodal.py`:
- Around line 176-199: Extend the existing
test_non_multimodal_model_accepts_text_files by adding a complementary negative
case that asserts non-text files are rejected for text-only models: call
llm._format_messages (same LLM(model="openai/gpt-3.5-turbo")) with a message
that contains an ImageFile (or other non-text file type) in the files dict and
wrap the call in pytest.raises(ValueError) to ensure a ValueError is thrown;
reference the existing test_non_multimodal_model_accepts_text_files, the
LLM._format_messages method, TextFile and ImageFile symbols, and
pytest.raises(ValueError) so both acceptance and rejection behaviors are locked
in.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 14f89687-655b-4400-bc8f-16ebc2278b26

📥 Commits

Reviewing files that changed from the base of the PR and between a95d267 and fe782e9.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/llms/base_llm.py
  • lib/crewai/tests/llms/test_multimodal.py

Comment thread lib/crewai/src/crewai/llms/base_llm.py
@pragnyanramtha pragnyanramtha force-pushed the pragnyan/inline-text-files-text-only-models-5137 branch from fe782e9 to 4699ad2 Compare May 16, 2026 16:59
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] / [HELP] "Model does not support multimodal input [...] Use a vision-capable model" for TextFile input

2 participants