-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Litellm anthropic image url support #16868
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
base: litellm_sameer_nov_3
Are you sure you want to change the base?
Litellm anthropic image url support #16868
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| assert '"bytes"' in json_str or '"bytes":' in json_str | ||
| elif model.startswith("anthropic/"): | ||
| # Direct Anthropic models should pass URLs directly | ||
| assert "https://upload.wikimedia.org" in json_str |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
https://upload.wikimedia.org
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 1 day ago
To avoid relying on substring checks that are prone to false positives, the test should deserialize the json_str payload, locate the relevant URL by traversing the structured data, and then assert that the expected host appears in the correct field. This can be achieved by parsing the JSON string, walking the nested structure to find the field(s) where the URL is expected, and then verifying that the expected hostname (e.g., upload.wikimedia.org) is present.
Concretely:
- Parse
json_strusingjson.loads. - Write a helper function to recursively search for values of "url" fields in the JSON and collect them.
- For the relevant test branches (e.g., where URLs should appear), assert that one of these "url" fields contains the expected domain and scheme, using url parsing (e.g., with
urllib.parse.urlparse), and verify the hostname matches exactly. - Use
inor string comparison only as needed on the exact field value, not the entire serialized payload.
Required steps:
- After obtaining
json_str, parse to a dictionary. - Add helper function to extract all "url" fields.
- In the relevant test branches, iterate and parse the extracted URLs, and assert on the hostname.
-
Copy modified lines R200-R215 -
Copy modified lines R226-R241
| @@ -197,7 +197,22 @@ | ||
| # bedrock/invoke models use Anthropic messages API which supports URLs | ||
| if model.startswith("bedrock/invoke/"): | ||
| # bedrock/invoke uses Anthropic messages API format, which supports URLs | ||
| assert "https://upload.wikimedia.org" in json_str | ||
| # Parse the JSON and verify that the "url" field contains the right host | ||
| data_obj = json.loads(json_str) | ||
| def find_urls(obj): | ||
| urls = [] | ||
| if isinstance(obj, dict): | ||
| for k, v in obj.items(): | ||
| if k == "url" and isinstance(v, str): | ||
| urls.append(v) | ||
| else: | ||
| urls.extend(find_urls(v)) | ||
| elif isinstance(obj, list): | ||
| for item in obj: | ||
| urls.extend(find_urls(item)) | ||
| return urls | ||
| urls = find_urls(data_obj) | ||
| assert any(urllib.parse.urlparse(u).hostname == "upload.wikimedia.org" for u in urls) | ||
| # For Anthropic, URL references use "url" type, not base64 | ||
| assert '"type":"url"' in json_str or '"type": "url"' in json_str | ||
| elif model.startswith("bedrock/"): | ||
| @@ -208,7 +223,22 @@ | ||
| assert '"bytes"' in json_str or '"bytes":' in json_str | ||
| elif model.startswith("anthropic/"): | ||
| # Direct Anthropic models should pass URLs directly | ||
| assert "https://upload.wikimedia.org" in json_str | ||
| # Parse the JSON and verify that the "url" field contains the right host | ||
| data_obj = json.loads(json_str) | ||
| def find_urls(obj): | ||
| urls = [] | ||
| if isinstance(obj, dict): | ||
| for k, v in obj.items(): | ||
| if k == "url" and isinstance(v, str): | ||
| urls.append(v) | ||
| else: | ||
| urls.extend(find_urls(v)) | ||
| elif isinstance(obj, list): | ||
| for item in obj: | ||
| urls.extend(find_urls(item)) | ||
| return urls | ||
| urls = find_urls(data_obj) | ||
| assert any(urllib.parse.urlparse(u).hostname == "upload.wikimedia.org" for u in urls) | ||
| # For Anthropic, URL references use "url" type, not base64 | ||
| assert '"type":"url"' in json_str or '"type": "url"' in json_str | ||
| else: |
60b1d43 to
5abe7d5
Compare
Title
Litellm anthropic image url support
Relevant issues
Fixes #12899
Fixes #16760
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🆕 New Feature
🐛 Bug Fix
Changes
Updated the code to handle image urls for anthropic