Coverage for app / services / provider_refusals.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""What a provider refused to take from this process, remembered until it 

2restarts. 

3 

4A request can ask a provider for more than an answer: to decode to a schema 

5(``structured_output``'s native path), to force the schema tool (its 

6function-calling path), to hold tool arguments to their schemas (strict tool 

7use). A provider that refuses one of those with a 400 refuses it again on the 

8next call, so the caller remembers the refusal and asks for less from then on 

9— for that model, that feature and, where the refusal is about one schema, 

10that schema. 

11 

12Only a refusal of the feature itself is remembered 

13(``model_errors.refused_schema`` / ``refused_tool_choice``). Any other 400 — 

14a prompt too long for the window, a parameter a model drops — falls back for 

15that call alone: remembering it would switch a feature off for the life of 

16the process because of one oversized request. 

17""" 

18 

19from __future__ import annotations 

20 

21from enum import StrEnum 

22 

23 

24class Feature(StrEnum): 

25 """What a request asked of the provider beyond an answer.""" 

26 

27 #: Constrained decoding to a JSON schema (``response_format`` / 

28 #: Converse ``outputConfig``); keyed by the schema's name. 

29 NATIVE_SCHEMA = "native_schema" 

30 #: Forcing the model to call the schema tool (``tool_choice``). 

31 FORCED_TOOL_CHOICE = "forced_tool_choice" 

32 #: Strict tool use (``toolSpec.strict``). 

33 STRICT_TOOLS = "strict_tools" 

34 

35 

36_REFUSED: set[tuple[str, str, Feature, str]] = set() 

37 

38 

39def remember(provider: str, model: str, feature: Feature, key: str = "") -> None: 

40 """Record that ``provider/model`` refused ``feature`` (for ``key``).""" 

41 _REFUSED.add((provider, model, feature, key)) 

42 

43 

44def refused(provider: str, model: str, feature: Feature, key: str = "") -> bool: 

45 """Whether ``provider/model`` has refused ``feature`` (for ``key``).""" 

46 return (provider, model, feature, key) in _REFUSED 

47 

48 

49def clear() -> None: 

50 """Forget every refusal (tests).""" 

51 _REFUSED.clear()