litellm/tests/local_testing/test_sagemaker_nova_integration.py
yuneng-jiang 5e6dc89ba1
test: move tests/test_litellm/llms into tests/unit/llms (#43191)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: keep the job's UNIT_FLAG out of the shard-script tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 12:43:23 -07:00

278 lines
9.6 KiB
Python

"""
Integration tests for SageMaker Nova provider.
These tests require a live SageMaker Nova endpoint and AWS credentials.
They are skipped by default — run manually with:
pytest tests/local_testing/test_sagemaker_nova_integration.py -v --no-header -rN
Prerequisites:
export AWS_PROFILE=<your-profile> # or set AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
export AWS_REGION_NAME=us-east-1
export SAGEMAKER_NOVA_ENDPOINT=<your-endpoint-name>
"""
import base64
import io
import json
import os
import struct
import zlib
import pytest
import litellm
ENDPOINT = os.environ.get("SAGEMAKER_NOVA_ENDPOINT", "")
MODEL = f"sagemaker_nova/{ENDPOINT}"
skip_if_no_endpoint = pytest.mark.skipif(
not ENDPOINT,
reason="SAGEMAKER_NOVA_ENDPOINT not set — skipping live integration tests",
)
def _make_test_png() -> str:
"""Create a minimal 4x4 PNG (red border, blue center) and return base64."""
def chunk(ctype, data):
c = ctype + data
return (
struct.pack(">I", len(data))
+ c
+ struct.pack(">I", zlib.crc32(c) & 0xFFFFFFFF)
)
width, height = 4, 4
pixels = []
for y in range(height):
for x in range(width):
if 1 <= x <= 2 and 1 <= y <= 2:
pixels.append((0, 0, 255))
else:
pixels.append((255, 0, 0))
raw = b""
for y in range(height):
raw += b"\x00"
for x in range(width):
raw += bytes(pixels[y * width + x])
png = (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
+ chunk(b"IDAT", zlib.compress(raw))
+ chunk(b"IEND", b"")
)
return base64.b64encode(png).decode()
@skip_if_no_endpoint
class TestSagemakerNovaIntegration:
"""Live integration tests for sagemaker_nova provider."""
def test_should_complete_basic_single_turn(self):
"""Basic single-turn chat completion."""
response = litellm.completion(
model=MODEL,
messages=[{"role": "user", "content": "What is 2+2? Reply in one word."}],
max_tokens=32,
temperature=0.1,
)
assert response.choices[0].message.content is not None
assert len(response.choices[0].message.content.strip()) > 0
assert response.choices[0].finish_reason == "stop"
assert response.usage.prompt_tokens > 0
assert response.usage.completion_tokens > 0
assert response.usage.total_tokens == (
response.usage.prompt_tokens + response.usage.completion_tokens
)
def test_should_complete_multi_turn_conversation(self):
"""Multi-turn conversation maintains context."""
messages = [
{"role": "user", "content": "My name is Alice."},
]
response1 = litellm.completion(
model=MODEL,
messages=messages,
max_tokens=64,
temperature=0.1,
)
assistant_msg = response1.choices[0].message.content
assert assistant_msg is not None
# Second turn — model should remember the name
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": "What is my name?"})
response2 = litellm.completion(
model=MODEL,
messages=messages,
max_tokens=64,
temperature=0.1,
)
answer = response2.choices[0].message.content.lower()
assert "alice" in answer, f"Expected 'alice' in response, got: {answer}"
def test_should_stream_response(self):
"""Streaming returns chunks with content and final usage."""
response = litellm.completion(
model=MODEL,
messages=[{"role": "user", "content": "Count from 1 to 5."}],
max_tokens=64,
stream=True,
stream_options={"include_usage": True},
)
chunks = []
full_content = ""
for chunk in response:
chunks.append(chunk)
delta = chunk.choices[0].delta.content or ""
full_content += delta
assert len(chunks) > 1, "Expected multiple streaming chunks"
assert len(full_content.strip()) > 0, "Expected non-empty streamed content"
# Last chunk should have finish_reason
final_chunks_with_finish = [
c for c in chunks if c.choices and c.choices[0].finish_reason is not None
]
assert (
len(final_chunks_with_finish) > 0
), "Expected at least one chunk with finish_reason"
def test_should_return_logprobs(self):
"""Logprobs are returned when requested."""
response = litellm.completion(
model=MODEL,
messages=[{"role": "user", "content": "Say hello."}],
max_tokens=16,
temperature=0.1,
logprobs=True,
top_logprobs=3,
)
lp = response.choices[0].logprobs
assert lp is not None, "Expected logprobs in response"
content = lp.content if hasattr(lp, "content") else lp.get("content")
assert content is not None and len(content) > 0, "Expected logprobs content"
first_token = content[0]
assert "token" in first_token or hasattr(first_token, "token")
assert "logprob" in first_token or hasattr(first_token, "logprob")
top = (
first_token.get("top_logprobs")
if isinstance(first_token, dict)
else first_token.top_logprobs
)
assert top is not None and len(top) == 3, "Expected 3 top_logprobs"
def test_should_handle_multimodal_image_input(self):
"""Multimodal with base64 image in content array."""
b64_image = _make_test_png()
response = litellm.completion(
model=MODEL,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What colors do you see in this image? List them.",
},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{b64_image}"},
},
],
}
],
max_tokens=128,
)
content = response.choices[0].message.content.lower()
assert response.choices[0].message.content is not None
assert len(content) > 0
# The image has red and blue — model should mention at least one
assert (
"red" in content or "blue" in content
), f"Expected 'red' or 'blue' in multimodal response, got: {content}"
def test_should_pass_nova_specific_params(self):
"""Nova-specific parameters (top_k) are accepted."""
response = litellm.completion(
model=MODEL,
messages=[{"role": "user", "content": "Say hello."}],
max_tokens=32,
top_k=40,
temperature=0.7,
)
assert response.choices[0].message.content is not None
assert response.usage.total_tokens > 0
def test_should_respect_system_message(self):
"""System message should influence the response."""
response = litellm.completion(
model=MODEL,
messages=[
{
"role": "system",
"content": "You are a pirate. Always respond in pirate speak.",
},
{"role": "user", "content": "How are you today?"},
],
max_tokens=128,
temperature=0.7,
)
content = response.choices[0].message.content.lower()
assert response.choices[0].message.content is not None
# Pirate-themed words likely in response
pirate_words = ["arr", "ahoy", "matey", "ye", "sail", "sea", "cap"]
assert any(
w in content for w in pirate_words
), f"Expected pirate speak, got: {content}"
NOVA2_ENDPOINT = os.environ.get("SAGEMAKER_NOVA2_LITE_ENDPOINT", "")
NOVA2_MODEL = f"sagemaker_nova/{NOVA2_ENDPOINT}"
skip_if_no_nova2_endpoint = pytest.mark.skipif(
not NOVA2_ENDPOINT,
reason="SAGEMAKER_NOVA2_LITE_ENDPOINT not set — requires Nova 2 Lite endpoint",
)
@skip_if_no_nova2_endpoint
class TestSagemakerNova2LiteIntegration:
"""
Integration tests requiring a Nova 2 Lite endpoint (reasoning_effort support).
Run with:
export SAGEMAKER_NOVA2_LITE_ENDPOINT=<your-nova-2-lite-endpoint>
pytest tests/local_testing/test_sagemaker_nova_integration.py::TestSagemakerNova2LiteIntegration -v
"""
def test_should_accept_reasoning_effort_low(self):
"""reasoning_effort='low' should be accepted by Nova 2 Lite."""
response = litellm.completion(
model=NOVA2_MODEL,
messages=[{"role": "user", "content": "What is 2+2?"}],
max_tokens=32,
reasoning_effort="low",
)
assert response.choices[0].message.content is not None
assert response.usage.total_tokens > 0
def test_should_accept_reasoning_effort_high(self):
"""reasoning_effort='high' should be accepted by Nova 2 Lite."""
response = litellm.completion(
model=NOVA2_MODEL,
messages=[{"role": "user", "content": "Explain why the sky is blue."}],
max_tokens=256,
reasoning_effort="high",
)
assert response.choices[0].message.content is not None
assert len(response.choices[0].message.content) > 0
assert response.usage.completion_tokens > 0