mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-04 14:26:18 +00:00
* bump: version 1.86.0 → 1.86.1 * chore: refresh uv.lock for 1.86.1 * fix(team): keep team_alias cache in sync on _cache_team_object writes (#28737) * fix(team): keep team_alias cache in sync on _cache_team_object writes _cache_team_object wrote only to the team_id:<id> cache key, but the JWT auth path that uses team_alias_jwt_field reads from a separate team_alias:<alias> key (get_team_object_by_alias caches under both keys on miss, but reads only the alias-keyed one). After any team-mutation endpoint (team_model_add, team_model_delete, update_team, the two access-group writes) the team_id cache was refreshed but the team_alias cache stayed stale until TTL — JWT callers using team_alias_jwt_field kept seeing the pre-mutation team for the full cache window. Mirror the write under the alias key inside _cache_team_object so every existing caller stays in sync without further changes. Skip the alias write when team_alias is None/empty so we don't collide across alias-less teams. Surfaced testing the LIT-3244 cherry-pick on patch/1.86.0: the LIT-3244 fix correctly invalidated the team_id cache but the customer's JWT used team_alias_jwt_field, so they kept hitting the stale alias-keyed entry. * fix(team): delete (not overwrite) team_alias cache on _cache_team_object The prior shape of this PR wrote both team_id:<id> AND team_alias:<alias> from _cache_team_object. team_alias is NOT unique in the schema (no @unique on LiteLLM_TeamTable.team_alias), and get_team_object_by_alias enforces uniqueness on its own DB-fetch path (len(teams) > 1 raises). Writing the alias-keyed cache from the generic refresh path bypassed that check: a team admin renaming their team to collide with another team's alias could silently overwrite the cached team for JWT-by-alias auth, swapping the resolved team under that alias for the cache window. Switch the alias-keyed operation from a write to a delete (mirroring the dual-cache delete pattern in _delete_cache_key_object). After every team write, the next JWT-by-alias reader cache-misses and falls through to get_team_object_by_alias, which (a) re-fetches the fresh team from DB, closing the LIT-3244 staleness gap that motivated this PR, and (b) enforces alias uniqueness before populating either cache key. team_id:<id> writes are unchanged — team_id is the table PK and is guaranteed unique. Surfaced in veria-ai review on #28739. * fix(managed-files): anchor model_id regex so it doesn't match llm_output_file_model_id extract_model_id_from_unified_id used `re.search(r"model_id,([^;]+)", ...)` which substring-matches the `model_id,` inside the file-ID encoding's `llm_output_file_model_id,<deployment_uuid>` field. parse_unified_id then fed that deployment UUID back into the auth path as a model candidate via _extract_models_from_managed_resource_id, and every team-BYOK file attach 403'd with: team not allowed to access model. This team can only access models=['openai/*']. Tried to access <deployment-uuid> The team's models list correctly contains the public name (`openai/*`) that target_model_names matches, but the bogus UUID candidate fails the wildcard check first. Anchor the regex to a field boundary (`(?:^|;)model_id,`) so it matches the legitimate top-level `model_id,<value>` field on vector_store unified IDs and skips substring matches inside other fields. File-IDs (which have no top-level `model_id` field) now return None and contribute no spurious UUID candidate. Surfaced reproducing LIT-3244 on patch/1.86.0 with the customer's exact flow: team with openai/* BYOK deployment, JWT-scoped user, POST /v1/vector_stores/{id}/files attaching a file uploaded with target_model_names=openai/gpt-4o. * fix(proxy): hydrate wildcard discovery credentials (#28284) * fix(proxy): hydrate wildcard discovery credentials * fix(proxy): constrain wildcard credential hydration * chore(tests): migrate Bedrock CI to AWS account 941277531214 (#28728) * chore(tests): migrate Bedrock CI from AWS account 888602223428 to 941277531214 The original account (888602223428) was put under a security restriction by AWS after a root access key leaked in a PR comment. While that account works its way through the AWS Support unlock process, Bedrock-touching CI tests have been migrated to a fresh account (941277531214). Changes: - Replace 26 hardcoded references to 888602223428 with 941277531214 across 8 files (provisioned-model ARNs, imported-model ARNs, AgentCore runtime ARNs, batch execution role ARN, and example proxy config). - The provisioned-model and imported-model ARNs are referenced only from mocked unit tests — no AWS resources to recreate. - The batch execution IAM role has been recreated in the new account with the same name and equivalent permissions. - The two AgentCore runtimes (hosted_agent_r9jvp-3ySZuRHjLC, hosted_agent_13sf6-cALnp38iZD) are being recreated in the new account under the same names — see tools/agentcore-deploy/ in a follow-up. CircleCI env vars AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_REGION_NAME were updated separately via the CircleCI API to point at the new account. Smoke-tested locally against the new account: aws bedrock-runtime converse --region us-west-2 \ --model-id us.anthropic.claude-sonnet-4-5-20250929-v1:0 \ --messages '[{"role":"user","content":[{"text":"ping"}]}]' → 200, model returned 'pong' Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore(tests): refresh AgentCore ARN suffixes to match newly-deployed runtimes The first migration commit replaced just the account ID, but AgentCore auto-assigns a random 10-char suffix to every runtime on creation — we can't reuse the original suffixes (`3ySZuRHjLC`, `cALnp38iZD`) in the new account. Updated the AgentCore-runtime ARNs in the three files that reference real runtime IDs (not the mock-based unit-test ARNs). Deployed runtimes: arn:aws:bedrock-agentcore:us-west-2:941277531214:runtime/hosted_agent_r9jvp-Rq79QFC2fp arn:aws:bedrock-agentcore:us-west-2:941277531214:runtime/hosted_agent_13sf6-4046UzHSwy Both runtimes are status=READY and pass a smoke invoke: $ aws bedrock-agentcore invoke-agent-runtime --agent-runtime-arn ... --payload '{"prompt":"ping"}' → 200, {"result": "echo: ping"} The agent is a minimal echo (see /tmp/agentcore_deploy/agent.py for the deploy artifacts). Tests that only verify the SDK wiring will pass; if any test asserts on agent output content, swap the echo for the real agent. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore(tests): point Bedrock batch tests at new-account S3 bucket The account migration (888602223428 -> 941277531214) was a flat account-ID swap, which only rewrites ARNs that embed the account number. S3 bucket names carry no account ID, so the live Bedrock batch tests still uploaded to `litellm-proxy` — a bucket that lives in the old account. S3 names are globally unique, and the old account still holds that name, so it can't be recreated in the new account. Rename to `litellm-proxy-941277531214` (account-ID suffix guarantees global uniqueness). The bucket must be created in 941277531214 and the batch execution role granted s3:GetObject/PutObject/ListBucket on it before this job is run in CI. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(tests): point live S3 logging test at new-account bucket Same account-ID-free blind spot as the batch bucket: `load-testing-oct` lives in the old account and its name can't be reused globally. The `logging_testing` CI job is wired into the workflow and runs test_basic_s3_logging, which uploads to this bucket with the CI env creds, then lists and deletes objects — a live dependency. Rename to `load-testing-oct-941277531214`. The bucket must exist in the new account with the CI IAM principal granted s3:PutObject/GetObject/ListBucket/DeleteObject before this job runs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(tests): repoint Bedrock guardrail IDs to new-account guardrails The migration left guardrail IDs untouched (no account ID in them), so all live guardrail tests failed with "guardrail identifier or version does not exist" against 941277531214. Recreated both guardrails in the new account and updated the hardcoded IDs: - wf0hkdb5x07f -> zgkmukebruil (PII mask: PHONE + CREDIT_DEBIT_CARD, with explicit inputAction=ANONYMIZE so masking applies to INPUT, which is the source litellm's moderation hook sends) - ff6ujrregl1q -> 4w3d1di3snt5 (blocks "coffee"; blocked message set to the exact string the tests assert on) Updated test_bedrock_guardrails.py, otel_test_config.yaml, and the guardrailConfig in test_bedrock_completion.py. Verified locally: the 5 previously-failing guardrail tests now pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(bedrock): migrate legacy models to current inference profiles The new CI account (941277531214) cannot invoke legacy Bedrock models (AWS gates them: "marked by provider as Legacy... not actively using in the last 30 days"). Migrated the live-call tests: - anthropic.claude-3-sonnet-20240229 -> us.anthropic.claude-sonnet-4-5-20250929-v1:0 - anthropic.claude-3-haiku-20240307 -> us.anthropic.claude-haiku-4-5-20251001-v1:0 Current Claude models on Bedrock require the us. inference-profile prefix (bare on-demand ids are rejected). cohere.command-r-plus has no working replacement (all Cohere is legacy- gated in the new account): swapped to claude-haiku-4-5 in provider- agnostic param lists. amazon.titan-image-generator skipped (no working replacement). Mocked/transformation/cost tests that reference the legacy strings are intentionally left unchanged. Verified live against the new account. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(bedrock): repoint SageMaker + Knowledge Base to new-account resources These referenced account-scoped resources by hardcoded id that only existed in the old account, so the migration's account-ID swap missed them. Recreated in 941277531214 and repointed: - SageMaker endpoint jumpstart-dft-hf-textgeneration1-mp-20240815-185614 -> litellm-ci-textgen (gpt2 on a TGI container, ml.g5.xlarge) - Bedrock Knowledge Base T37J8R4WTM -> LCYXFBR2TU (OpenSearch Serverless vector store + titan-embed-text-v2, seeded with a LiteLLM doc) Verified live: test_sagemaker.py (12 passed) and test_bedrock_knowledgebase_hook.py (12 passed). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(reasoning_effort_grid): skip bedrock claude-opus-4-7 cells (not entitled on 941277531214) claude-opus-4-7 is listed in the new Bedrock CI account's foundation models but invoke is denied (AccessDeniedException: "not available for this account"). Bedrock access to the flagship Opus requires an AWS Sales request, not the self-serve model-access toggle, so it can't be enabled inline with the rest of the account migration. Add an optional `skip_reason` to ModelEntry and set it on the bedrock-claude-opus-4-7 entry; the grid test honors it via pytest.skip. Cell count (231) and route coverage are unchanged, so the structural asserts still pass. Restore coverage by deleting the one skip_reason line once access is granted. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(bedrock): swap/skip legacy-gated models unavailable on new CI account The migrated AWS account (941277531214) cannot access several models that the old account could, so the remaining red CI jobs were hitting real Bedrock "Access denied / Legacy" and "account not authorized" errors: - image_gen: skip both Nova Canvas test classes (amazon.nova-canvas-v1:0 is legacy-gated), matching the existing titan skip. - batches: skip test_async_file_and_batch (Bedrock batch inference is not authorized on the new account; requires an AWS support case). - litellm_overhead: swap legacy claude-3-5-haiku for the active us.anthropic.claude-haiku-4-5 inference profile. - test_completion_claude_3_function_call: swap legacy claude-3-sonnet for the active us.anthropic.claude-sonnet-4-5 inference profile. https://claude.ai/code/session_01Y7zgHYu9GX29YRwV4yiWAa * test(bedrock): fix remaining e2e legacy-model + batch failures on new CI account - e2e_openai_endpoints: skip test_bedrock_batches_api (Bedrock batch inference is not authorized on account 941277531214) and migrate the missed s3_bucket_name in oai_misc_config.yaml to litellm-proxy-941277531214. - build_and_test: swap legacy bedrock claude-3-sonnet for the active us.anthropic.claude-sonnet-4-5 inference profile in the proxy structured output e2e test. https://claude.ai/code/session_01Y7zgHYu9GX29YRwV4yiWAa * test(bedrock): make opus-4-7 + batch cells fail loudly and mock image-gen (#28791) Replace the silent skips added for the new CI account with noisier behavior: - reasoning-effort grid: opus-4-7 cells now fail (when AWS creds are present) instead of skipping, so the missing entitlement stays visible in CI; they still skip when AWS creds are absent (local dev) - Bedrock batch inference tests: drop the skip so they run and fail until batch access is granted - Titan + Nova Canvas image-gen tests: mock the Bedrock HTTP call so the transform + cost-tracking path stays under test without live model access https://claude.ai/code/session_01MT7SWDnXUjv6e6EPG7BDjT Co-authored-by: Claude <noreply@anthropic.com> * test(bedrock): use pytest.xfail for known-failing opus-4-7 cells Replace pytest.fail with pytest.xfail when a model has a fail_reason, so known-broken cells stay visible as XFAIL without keeping CI red. Co-authored-by: Yassin Kortam <yassin@berri.ai> --------- Co-authored-by: Mateo <mateo@Mateos-MacBook-Pro.local> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Yassin Kortam <yassin@berri.ai> --------- Co-authored-by: Dibyo Mukherjee <dibyo@adobe.com> Co-authored-by: Mateo Wang <277851410+mateo-berri@users.noreply.github.com> Co-authored-by: Mateo <mateo@Mateos-MacBook-Pro.local> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Yassin Kortam <yassin@berri.ai>
474 lines
18 KiB
Python
474 lines
18 KiB
Python
# What this tests?
|
|
## This tests the litellm support for the openai /generations endpoint
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
import traceback
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
sys.path.insert(
|
|
0, os.path.abspath("../..")
|
|
) # Adds the parent directory to the system path
|
|
|
|
from dotenv import load_dotenv
|
|
from openai.types.image import Image
|
|
from litellm.caching import InMemoryCache
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
load_dotenv()
|
|
import asyncio
|
|
import os
|
|
import pytest
|
|
|
|
import litellm
|
|
import json
|
|
import tempfile
|
|
from base_image_generation_test import BaseImageGenTest, TestCustomLogger
|
|
import logging
|
|
from litellm._logging import verbose_logger
|
|
|
|
verbose_logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
def get_vertex_ai_creds_json() -> dict:
|
|
# Define the path to the vertex_key.json file
|
|
print("loading vertex ai credentials")
|
|
filepath = os.path.dirname(os.path.abspath(__file__))
|
|
vertex_key_path = filepath + "/vertex_key.json"
|
|
# Read the existing content of the file or create an empty dictionary
|
|
try:
|
|
with open(vertex_key_path, "r") as file:
|
|
# Read the file content
|
|
print("Read vertexai file path")
|
|
content = file.read()
|
|
|
|
# If the file is empty or not valid JSON, create an empty dictionary
|
|
if not content or not content.strip():
|
|
service_account_key_data = {}
|
|
else:
|
|
# Attempt to load the existing JSON content
|
|
file.seek(0)
|
|
service_account_key_data = json.load(file)
|
|
except FileNotFoundError:
|
|
# If the file doesn't exist, create an empty dictionary
|
|
service_account_key_data = {}
|
|
|
|
# Update the service_account_key_data with environment variables
|
|
private_key_id = os.environ.get("VERTEX_AI_PRIVATE_KEY_ID", "")
|
|
private_key = os.environ.get("VERTEX_AI_PRIVATE_KEY", "")
|
|
private_key = private_key.replace("\\n", "\n")
|
|
service_account_key_data["private_key_id"] = private_key_id
|
|
service_account_key_data["private_key"] = private_key
|
|
|
|
return service_account_key_data
|
|
|
|
|
|
def load_vertex_ai_credentials():
|
|
# Define the path to the vertex_key.json file
|
|
print("loading vertex ai credentials")
|
|
filepath = os.path.dirname(os.path.abspath(__file__))
|
|
vertex_key_path = filepath + "/vertex_key.json"
|
|
|
|
# Read the existing content of the file or create an empty dictionary
|
|
try:
|
|
with open(vertex_key_path, "r") as file:
|
|
# Read the file content
|
|
print("Read vertexai file path")
|
|
content = file.read()
|
|
|
|
# If the file is empty or not valid JSON, create an empty dictionary
|
|
if not content or not content.strip():
|
|
service_account_key_data = {}
|
|
else:
|
|
# Attempt to load the existing JSON content
|
|
file.seek(0)
|
|
service_account_key_data = json.load(file)
|
|
except FileNotFoundError:
|
|
# If the file doesn't exist, create an empty dictionary
|
|
service_account_key_data = {}
|
|
|
|
# Update the service_account_key_data with environment variables
|
|
private_key_id = os.environ.get("VERTEX_AI_PRIVATE_KEY_ID", "")
|
|
private_key = os.environ.get("VERTEX_AI_PRIVATE_KEY", "")
|
|
private_key = private_key.replace("\\n", "\n")
|
|
service_account_key_data["private_key_id"] = private_key_id
|
|
service_account_key_data["private_key"] = private_key
|
|
|
|
# Create a temporary file
|
|
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp_file:
|
|
# Write the updated content to the temporary files
|
|
json.dump(service_account_key_data, temp_file, indent=2)
|
|
|
|
# Export the temporary file as GOOGLE_APPLICATION_CREDENTIALS
|
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.abspath(temp_file.name)
|
|
|
|
|
|
class TestVertexImageGeneration(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
# comment this when running locally
|
|
load_vertex_ai_credentials()
|
|
|
|
litellm.in_memory_llm_clients_cache = InMemoryCache()
|
|
return {
|
|
"model": "vertex_ai/imagen-3.0-fast-generate-001",
|
|
"vertex_ai_project": "litellm-ci-cd",
|
|
"vertex_ai_location": "us-central1",
|
|
"n": 1,
|
|
}
|
|
|
|
|
|
class TestVertexAIGeminiImageGeneration(BaseImageGenTest):
|
|
"""Test Gemini image generation models (Nano Banana)"""
|
|
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
# comment this when running locally
|
|
load_vertex_ai_credentials()
|
|
|
|
litellm.in_memory_llm_clients_cache = InMemoryCache()
|
|
return {
|
|
"model": "vertex_ai/gemini-2.5-flash-image",
|
|
"vertex_ai_project": "litellm-ci-cd",
|
|
"vertex_ai_location": "us-central1",
|
|
"n": 1,
|
|
"size": "1024x1024",
|
|
}
|
|
|
|
|
|
# Base64 placeholder used for mocked Bedrock image responses (a 1x1 PNG).
|
|
_MOCK_BEDROCK_IMAGE_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
|
|
|
|
|
|
async def _assert_mocked_bedrock_image_generation(call_args: dict) -> None:
|
|
"""Run ``aimage_generation`` with the Bedrock HTTP call mocked.
|
|
|
|
The CI account is not entitled to Nova Canvas, so the network call is
|
|
replaced with a canned Bedrock response. This keeps the request transform,
|
|
response transform, and cost-tracking path under test without live access.
|
|
"""
|
|
mock_payload = {"images": [_MOCK_BEDROCK_IMAGE_B64]}
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = mock_payload
|
|
mock_response.text = json.dumps(mock_payload)
|
|
mock_response.headers = {}
|
|
|
|
custom_logger = TestCustomLogger()
|
|
litellm.logging_callback_manager._reset_all_callbacks()
|
|
litellm.callbacks = [custom_logger]
|
|
|
|
with patch(
|
|
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
|
new_callable=AsyncMock,
|
|
return_value=mock_response,
|
|
):
|
|
response = await litellm.aimage_generation(
|
|
**call_args,
|
|
prompt="A image of a otter",
|
|
aws_access_key_id="fake-access-key-id",
|
|
aws_secret_access_key="fake-secret-access-key",
|
|
)
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
assert custom_logger.standard_logging_payload is not None
|
|
assert custom_logger.standard_logging_payload["response_cost"] is not None
|
|
assert custom_logger.standard_logging_payload["response_cost"] > 0
|
|
assert response.data is not None
|
|
for d in response.data:
|
|
assert isinstance(d, Image)
|
|
assert d.b64_json is not None or d.url is not None
|
|
|
|
|
|
class TestBedrockNovaCanvasTextToImage(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
litellm.in_memory_llm_clients_cache = InMemoryCache()
|
|
return {
|
|
"model": "bedrock/amazon.nova-canvas-v1:0",
|
|
"n": 1,
|
|
"size": "320x320",
|
|
"imageGenerationConfig": {"cfgScale": 6.5, "seed": 12},
|
|
"taskType": "TEXT_IMAGE",
|
|
"aws_region_name": "us-east-1",
|
|
}
|
|
|
|
@pytest.mark.asyncio(scope="module")
|
|
async def test_basic_image_generation(self):
|
|
await _assert_mocked_bedrock_image_generation(
|
|
self.get_base_image_generation_call_args()
|
|
)
|
|
|
|
|
|
class TestBedrockNovaCanvasColorGuidedGeneration(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
litellm.in_memory_llm_clients_cache = InMemoryCache()
|
|
return {
|
|
"model": "bedrock/amazon.nova-canvas-v1:0",
|
|
"n": 1,
|
|
"size": "320x320",
|
|
"imageGenerationConfig": {"cfgScale": 6.5, "seed": 12},
|
|
"taskType": "COLOR_GUIDED_GENERATION",
|
|
"colorGuidedGenerationParams": {"colors": ["#FFFFFF"]},
|
|
"aws_region_name": "us-east-1",
|
|
}
|
|
|
|
@pytest.mark.asyncio(scope="module")
|
|
async def test_basic_image_generation(self):
|
|
await _assert_mocked_bedrock_image_generation(
|
|
self.get_base_image_generation_call_args()
|
|
)
|
|
|
|
|
|
class TestOpenAIGPTImage1(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
return {"model": "gpt-image-1"}
|
|
|
|
|
|
@pytest.mark.skip(reason="Recraft image generation API only tested locally")
|
|
class TestRecraftImageGeneration(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
return {"model": "recraft/recraftv3"}
|
|
|
|
|
|
class TestAimlImageGeneration(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
return {"model": "aiml/flux-pro/v1.1"}
|
|
|
|
@pytest.mark.asyncio(scope="module")
|
|
@pytest.mark.flaky(retries=0)
|
|
async def test_basic_image_generation(self):
|
|
"""Test basic image generation"""
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
mock_aiml_response = {
|
|
"created": 1703658209,
|
|
"data": [{"url": "https://example.com/generated_image.png"}],
|
|
}
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = mock_aiml_response
|
|
mock_response.text = json.dumps(mock_aiml_response)
|
|
mock_response.headers = {}
|
|
|
|
with (
|
|
patch(
|
|
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
|
new_callable=AsyncMock,
|
|
) as mock_async_post,
|
|
patch(
|
|
"litellm.llms.custom_httpx.http_handler.HTTPHandler.post",
|
|
) as mock_sync_post,
|
|
):
|
|
mock_async_post.return_value = mock_response
|
|
mock_sync_post.return_value = mock_response
|
|
|
|
try:
|
|
litellm._turn_on_debug()
|
|
custom_logger = TestCustomLogger()
|
|
litellm.logging_callback_manager._reset_all_callbacks()
|
|
litellm.callbacks = [custom_logger]
|
|
base_image_generation_call_args = (
|
|
self.get_base_image_generation_call_args()
|
|
)
|
|
litellm.set_verbose = True
|
|
# Pass dummy api_key so validate_environment passes; HTTP is mocked
|
|
response = await litellm.aimage_generation(
|
|
**base_image_generation_call_args,
|
|
prompt="A image of a otter",
|
|
api_key="test-key-mocked-no-credits-needed",
|
|
)
|
|
print("FAL AI RESPONSE: ", response)
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
# assert response._hidden_params["response_cost"] is not None
|
|
# assert response._hidden_params["response_cost"] > 0
|
|
# print("response_cost", response._hidden_params["response_cost"])
|
|
|
|
logged_standard_logging_payload = custom_logger.standard_logging_payload
|
|
print(
|
|
"logged_standard_logging_payload", logged_standard_logging_payload
|
|
)
|
|
assert logged_standard_logging_payload is not None
|
|
assert logged_standard_logging_payload["response_cost"] is not None
|
|
assert logged_standard_logging_payload["response_cost"] > 0
|
|
import openai
|
|
from openai.types.images_response import ImagesResponse
|
|
|
|
# print openai version
|
|
print("openai version=", openai.__version__)
|
|
|
|
response_dict = dict(response)
|
|
if "usage" in response_dict:
|
|
response_dict["usage"] = dict(response_dict["usage"])
|
|
print("response usage=", response_dict.get("usage"))
|
|
|
|
assert (
|
|
response.data is not None
|
|
) # type guard for iteration (base fails here if None)
|
|
for d in response.data:
|
|
assert isinstance(d, Image)
|
|
print("data in response.data", d)
|
|
assert d.b64_json is not None or d.url is not None
|
|
except litellm.RateLimitError as e:
|
|
pass
|
|
except litellm.ContentPolicyViolationError:
|
|
pass # Azure randomly raises these errors - skip when they occur
|
|
except litellm.InternalServerError:
|
|
pass
|
|
except Exception as e:
|
|
if "Your task failed as a result of our safety system." in str(e):
|
|
pass
|
|
else:
|
|
pytest.fail(f"An exception occurred - {str(e)}")
|
|
|
|
|
|
class TestGoogleImageGen(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
return {"model": "gemini/imagen-4.0-generate-001"}
|
|
|
|
|
|
@pytest.mark.skip(reason="Runwayml image generation API only tested locally")
|
|
class TestRunwaymlImageGeneration(BaseImageGenTest):
|
|
def get_base_image_generation_call_args(self) -> dict:
|
|
return {"model": "runwayml/gen4_image"}
|
|
|
|
|
|
## AZURE AI DALL-E 3 is deprecated and new deployments cannot be made
|
|
# class TestAzureOpenAIDalle3(BaseImageGenTest):
|
|
# def get_base_image_generation_call_args(self) -> dict:
|
|
# return {
|
|
# "model": "azure/dall-e-3",
|
|
# "api_version": "2024-02-01",
|
|
# "api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
# "api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
# "metadata": {
|
|
# "model_info": {
|
|
# "base_model": "azure/dall-e-3",
|
|
# }
|
|
# },
|
|
# }
|
|
|
|
|
|
@pytest.mark.skip(reason="model EOL")
|
|
@pytest.mark.asyncio
|
|
async def test_aimage_generation_bedrock_with_optional_params():
|
|
try:
|
|
litellm.in_memory_llm_clients_cache = InMemoryCache()
|
|
response = await litellm.aimage_generation(
|
|
prompt="A cute baby sea otter",
|
|
model="bedrock/stability.stable-diffusion-xl-v1",
|
|
size="256x256",
|
|
)
|
|
print(f"response: {response}")
|
|
except litellm.RateLimitError as e:
|
|
pass
|
|
except litellm.ContentPolicyViolationError:
|
|
pass # Azure randomly raises these errors skip when they occur
|
|
except Exception as e:
|
|
if "Your task failed as a result of our safety system." in str(e):
|
|
pass
|
|
else:
|
|
pytest.fail(f"An exception occurred - {str(e)}")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aiml_image_generation_with_dynamic_api_key():
|
|
"""
|
|
Test that when api_key is passed as a dynamic parameter to aimage_generation,
|
|
it gets properly used for AIML provider authentication instead of falling back
|
|
to environment variables.
|
|
|
|
This test validates the fix for ensuring dynamic API keys are respected
|
|
when making image generation requests to the AIML provider.
|
|
"""
|
|
from unittest.mock import AsyncMock, patch, MagicMock
|
|
import httpx
|
|
|
|
# Mock AIML response
|
|
mock_aiml_response = {
|
|
"created": 1703658209,
|
|
"data": [{"url": "https://example.com/generated_image.png"}],
|
|
}
|
|
|
|
# Track captured arguments
|
|
captured_headers = None
|
|
captured_url = None
|
|
captured_json_data = None
|
|
|
|
def capture_post_call(*args, **kwargs):
|
|
nonlocal captured_headers, captured_url, captured_json_data
|
|
captured_url = kwargs.get("url") or (args[0] if args else None)
|
|
captured_headers = kwargs.get("headers", {})
|
|
captured_json_data = kwargs.get("json", {})
|
|
|
|
# Create a mock response
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = mock_aiml_response
|
|
mock_response.text = json.dumps(mock_aiml_response)
|
|
return mock_response
|
|
|
|
# Mock the HTTP client that actually makes the request (sync version for image generation)
|
|
with patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post") as mock_post:
|
|
mock_post.side_effect = capture_post_call
|
|
|
|
# Test with dynamic api_key
|
|
test_api_key = "test-dynamic-api-key-12345"
|
|
|
|
response = await litellm.aimage_generation(
|
|
prompt="A cute baby sea otter",
|
|
model="aiml/flux-pro/v1.1",
|
|
api_key=test_api_key, # This should be used instead of env vars
|
|
)
|
|
|
|
# Validate the response (mocked response processing might not populate data correctly)
|
|
assert response is not None
|
|
|
|
# The most important validations: API key and endpoint usage
|
|
# These prove that the dynamic API key was properly used
|
|
assert captured_headers is not None
|
|
assert "Authorization" in captured_headers
|
|
assert captured_headers["Authorization"] == f"Bearer {test_api_key}"
|
|
print("TESTCAPTURED HEADERS", captured_headers)
|
|
# Validate the correct AIML endpoint was called
|
|
assert captured_url is not None
|
|
assert "api.aimlapi.com" in captured_url
|
|
assert "/v1/images/generations" in captured_url
|
|
|
|
# Validate the request data
|
|
assert captured_json_data is not None
|
|
assert captured_json_data["prompt"] == "A cute baby sea otter"
|
|
assert captured_json_data["model"] == "flux-pro/v1.1"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_azure_image_generation_request_body():
|
|
"""Azure deployment URL selects the model; JSON body omits ``model`` (#26316)."""
|
|
from litellm import aimage_generation
|
|
|
|
test_dir = os.path.dirname(__file__)
|
|
expected_path = os.path.join(test_dir, "request_payloads", "azure_gpt_image_1.json")
|
|
with open(expected_path, "r") as f:
|
|
expected_body = json.load(f)
|
|
|
|
with patch(
|
|
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
|
new_callable=AsyncMock,
|
|
) as mock_post:
|
|
mock_post.side_effect = Exception("test")
|
|
|
|
with pytest.raises(Exception):
|
|
await aimage_generation(
|
|
model="azure/gpt-image-1",
|
|
prompt="test prompt",
|
|
api_base="https://example.azure.com",
|
|
api_key="test-key",
|
|
api_version="2025-04-01-preview",
|
|
)
|
|
|
|
mock_post.assert_called_once()
|
|
call_args = mock_post.call_args
|
|
request_json = call_args.kwargs.get("json", {})
|
|
assert request_json == expected_body
|