Files
wehub-resource-sync 97e91a83f3
Ruff / Ruff (push) Waiting to run
Test / Core Tests (push) Waiting to run
Test / Offline Coverage Tests (Python 3.10) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.11) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.12) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.13) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.9) (push) Waiting to run
Test / Full Coverage (Python 3.11) (push) Waiting to run
Test / Core Provider Tests (OpenAI) (push) Blocked by required conditions
Test / Core Provider Tests (Anthropic) (push) Blocked by required conditions
Test / Core Provider Tests (Google) (push) Blocked by required conditions
Test / Core Provider Tests (Other) (push) Blocked by required conditions
Test / Anthropic Tests (push) Blocked by required conditions
Test / Gemini Tests (push) Blocked by required conditions
Test / Google GenAI Tests (push) Blocked by required conditions
Test / Vertex AI Tests (push) Blocked by required conditions
Test / OpenAI Tests (push) Blocked by required conditions
Test / Writer Tests (push) Blocked by required conditions
Test / Auto Client Tests (push) Blocked by required conditions
ty / type-check (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:36:38 +08:00

46 lines
1.7 KiB
Python
Raw Permalink 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"