[FIX] [litellm_wrapper] omit empty text block for image-only vision calls - #1712
Closed
ebarkhordar wants to merge 1 commit into
Closed
[FIX] [litellm_wrapper] omit empty text block for image-only vision calls#1712ebarkhordar wants to merge 1 commit into
ebarkhordar wants to merge 1 commit into
Conversation
|
Hello there, thank you for opening an PR ! 🙏🏻 The team was notified and they will get back to you asap. |
…alls
The vision message builders always prepended {"type": "text", "text": task},
so an image-only call (empty or None task) produced a user message containing
an empty text content block. Anthropic rejects empty text blocks; the wrapper
already drops empty system blocks in _prepare_messages for the same reason,
and the vision user-text block was the ungated case in that family.
Add a _vision_content(task, image_block) helper that includes the text block
only when task is a non-empty string, and route the anthropic (direct-URL and
base64) and openai vision builders through it. Behavior is unchanged when a
task is present.
ebarkhordar
force-pushed
the
fix/vision-empty-text-block
branch
from
July 13, 2026 08:44
d4879ba to
cc9a573
Compare
Author
|
No rush on this. It is a small litellm_wrapper fix (drop the empty text block for image-only vision calls, since the provider rejects a bare empty content block), and I am happy to rebase or add a test if that would help you review. |
Author
|
Closing this to keep my open PR list at a size I can actually shepherd. Nothing here is The change still applies if it is useful later, and I am happy to reopen and rebase it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
LiteLLM's vision message builders always prepended a text content block, evenwhen the task was empty or
None. An image-only call such asllm.run("", img=...)or
run(None, img=...)therefore built a user message containing{"type": "text", "text": ""}next to the image block.Root cause
anthropic_vision_processing(both the direct-URL and base64 branches) andopenai_vision_processingunconditionally start the content list with{"type": "text", "text": task}. Whentaskis empty orNone, that block isempty.
Invariant
No content block in a vision message should carry empty text. The wrapper already
enforces this for system blocks in
_prepare_messages, which drops empty systemblocks with the note that Anthropic rejects them ("system: text content blocks
must be non-empty"). The vision user-text block was the one spot in the same
family that was not guarded.
Fix
Add a small
_vision_content(task, image_block)helper that includes the textblock only when
taskis a non-empty (non-whitespace) string, then route allthree construction sites through it. With no task the message carries just the
image block; with a task, behavior is unchanged. This mirrors the existing
empty-system-block normalization.
Verification
Ran in a clean Docker container (python:3.12-slim, litellm==1.76.1), offline
(
--network none), against this branch and against master:tests/utils/test_litellm_vision_empty_text.py: the 7 image-onlycases (empty / whitespace / None task, Anthropic and OpenAI paths, plus a
forced direct-URL case) FAIL on master (the empty
{"type": "text", "text": ""}block is present) and PASS on this branch.
so the fix does not drop legitimate text.
The tests assert the message structure the wrapper builds. I did not make a live
Anthropic API call, so I have not personally observed the rejection; the premise
rests on Anthropic's documented "text content blocks must be non-empty"
constraint and on this repo's own existing system-block guard for it.
Black (24.2.0) and Ruff (0.2.1) are clean on both changed files.
Prepared with AI assistance; reproduced and verified as described above.