Files
567-labs--instructor/tests/cache/test_cache_integration.py
T
wehub-resource-sync 97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:36:38 +08:00

46 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import types
import instructor
from instructor.cache import AutoCache
from openai.types.chat import ChatCompletionMessageParam
from pydantic import BaseModel, Field # type: ignore[import-not-found]
def test_auto_cache_prevents_duplicate_provider_calls(monkeypatch):
_ = monkeypatch # unused fixture for parity with other tests
"""Ensure that AutoCache prevents duplicate provider calls via patch layer."""
class User(BaseModel):
name: str = Field(...)
call_counter = {"n": 0}
# Fake provider completion function mimicking minimal OpenAI chat response
def fake_completion(*_args, **_kwargs): # noqa: D401, ANN001
call_counter["n"] += 1
content = User(name="cached").model_dump_json()
# Return minimal ChatCompletion-like object
return types.SimpleNamespace(
choices=[
types.SimpleNamespace(
message=types.SimpleNamespace(content=content),
finish_reason="stop",
)
],
usage={},
)
# Create Instructor client using from_litellm so we go through patch stack
cache = AutoCache(maxsize=10)
client = instructor.from_litellm(fake_completion, mode=instructor.Mode.JSON)
messages: list[ChatCompletionMessageParam] = [{"role": "user", "content": "hello"}]
# First call provider should be invoked
_ = client.create(messages=list(messages), response_model=User, cache=cache)
assert call_counter["n"] == 1
# Second call with identical inputs should hit cache, no new provider call
_ = client.create(messages=list(messages), response_model=User, cache=cache)
assert call_counter["n"] == 1, "Cache miss provider was called again"