diff --git a/docs/cookbooks/20-judgments.md b/docs/cookbooks/20-judgments.md index 057ccb2b..e534cb66 100644 --- a/docs/cookbooks/20-judgments.md +++ b/docs/cookbooks/20-judgments.md @@ -99,3 +99,35 @@ df["quality"] = pd.Categorical(df.quality, categories=range(5), ordered=True) - Pictures: `Message.user(image(path="door.jpg"))` makes the image Jev's state; `Message.user(["Listing: a red mug.", image(path="a.png"), image(path="b.jpg")])` sends the parts in order, as an array (a question can say "the second image" or point at `` `[2]` ``). PNG and JPEG bytes only: a URL, a file id, another format, audio or video is refused before the wire. lm15 sends TypeSafe's `TypeSafe-Danger-Zone-Images: true` header for you; TypeSafe calls images beta, so check answers on pictures you know. `lm15-contract/changes/2026-10-14-jev-images.md`. - Bounding boxes: `lm15-contract/changes/2026-10-15-bounding-box.md`. - Contract: `lm15-contract/changes/2026-09-17-judgments.md`, MAP-14. + + +## OpenAI Decisions + +Select `openai-decisions:gpt-6-luna` to send judgments to `/v1/decisions` +using `OPENAI_API_KEY`. The ordinary `openai:` route still uses Responses. +Boolean judgments become predicates; choices and ordered levels retain their +probability distributions. `response.expected(name)` reads an ordered score's +expected value; `response.data[name]` is its most likely level. + +```python +from lm15 import LMRouter, Request, Message, Config, judgments, yes_no + +with LMRouter() as router: + result = router.complete(Request( + model="openai-decisions:gpt-6-luna", + messages=(Message.user("The package arrived damaged."),), + config=Config(response_format=judgments(damaged=yes_no("Does the message report damage?"))), + )) +print(result.probabilities["damaged"]) +``` + +`AsyncLMRouter` uses the same codec. Text, JSON data, and image user inputs are +supported, together with text conversation history. Streaming, tool-call +history, bounding boxes, free-form outputs, generation controls, and explicit +`Config.cache` controls are rejected. The endpoint's implicit prompt caching +still applies; native cache options can be supplied through `Config.extensions`. +Refusals retain other answers and usage, set `finish_reason="content_filter"`, +and name refused questions in `provider_data["decisions"]["refusals"]`. +The original reply is retained under `provider_data["openai-decisions"]`. + +See the [OpenAI Decisions guide](https://developers.openai.com/api/docs/guides/decisions). diff --git a/lm15/__init__.py b/lm15/__init__.py index dd3306f4..17c760d6 100644 --- a/lm15/__init__.py +++ b/lm15/__init__.py @@ -182,6 +182,7 @@ OpenAICodexLM, XaiLM, TypeSafeLM, + OpenAIDecisionsLM, ) from .providers.openai_chat import request_from_openai_chat, response_from_openai_chat from .protocols import ProviderLM @@ -194,6 +195,7 @@ AsyncOpenAICodexLM, AsyncXaiLM, AsyncTypeSafeLM, + AsyncOpenAIDecisionsLM, ) # ── Stream assembly ────────────────────────────────────────────────── @@ -269,7 +271,7 @@ "Auth", "AsyncAuth", "BoundClient", "TerminalUI", "providers", "connect", # providers "bounding_box", "choice", "judgments", "score", "yes_no", "data", - "OpenAILM", "OpenAIChatLM", "AnthropicLM", "GeminiLM", "ClaudeCodeLM", "OpenAICodexLM", "XaiLM", "TypeSafeLM", "AsyncTypeSafeLM", + "OpenAILM", "OpenAIChatLM", "AnthropicLM", "GeminiLM", "ClaudeCodeLM", "OpenAICodexLM", "XaiLM", "TypeSafeLM", "AsyncTypeSafeLM", "OpenAIDecisionsLM", "AsyncOpenAIDecisionsLM", "ProviderLM", # the Chat Completions dialect read in: a request body -> Request (MAP-12), a response body -> Response "request_from_openai_chat", "response_from_openai_chat", diff --git a/lm15/access.py b/lm15/access.py index 79363c9a..f1e493a3 100644 --- a/lm15/access.py +++ b/lm15/access.py @@ -157,6 +157,13 @@ base_url=DEFAULT_CODEX_BASE_URL, ) +OPENAI_DECISIONS_API = AccessPolicy( + provider="openai-decisions", + supports=EndpointSupport(complete=True, stream=False), + auth_modes=("bearer",), + env_keys=("OPENAI_API_KEY",), +) + OPENAI_CHAT_API = AccessPolicy( provider="openai-chat", supports=EndpointSupport(complete=True, stream=True, models=True), diff --git a/lm15/providers/__init__.py b/lm15/providers/__init__.py index 2dc45bab..3df72db7 100644 --- a/lm15/providers/__init__.py +++ b/lm15/providers/__init__.py @@ -9,6 +9,7 @@ AsyncOpenAILM, AsyncTransport, AsyncTypeSafeLM, + AsyncOpenAIDecisionsLM, AsyncXaiLM, ) from .base import BaseProviderLM, Credential, HttpResponse, ProviderDialect, SyncTransport, resolve_credential @@ -18,6 +19,7 @@ from .openai_chat import OpenAIChatLM from .openai_codex import OpenAICodexLM from .typesafe import TypeSafeLM +from .openai_decisions import OpenAIDecisionsLM from .xai import XaiLM __all__ = [ @@ -30,6 +32,8 @@ "XaiLM", "AsyncXaiLM", "TypeSafeLM", + "OpenAIDecisionsLM", + "AsyncOpenAIDecisionsLM", "AsyncTypeSafeLM", "AsyncOpenAILM", "AsyncOpenAIChatLM", diff --git a/lm15/providers/async_base.py b/lm15/providers/async_base.py index 8d4489de..a1122978 100644 --- a/lm15/providers/async_base.py +++ b/lm15/providers/async_base.py @@ -62,6 +62,7 @@ OpenAICodexLM, ) from .typesafe import TypeSafeLM +from .openai_decisions import OpenAIDecisionsLM from .xai import DEFAULT_XAI_BASE_URL, XaiLM _T = TypeVar("_T") @@ -1068,3 +1069,36 @@ def __post_init__(self) -> None: clock=self.clock, ) self._mirror_binding() + + +@dataclass +class AsyncOpenAIDecisionsLM(AsyncBaseProviderLM): + """Async mirror of :class:`OpenAIDecisionsLM` (composition; the inner adapter + cannot reach the network).""" + + api_key: Credential | None = field(default=None, repr=False) + transport: AsyncTransport = field(default_factory=default_async_transport) + base_url: str = "https://api.openai.com/v1" + access: ProviderManifest = field(default=OpenAIDecisionsLM.manifest, repr=False) + credentials_path: "str | os.PathLike[str] | None" = field(default=None, repr=False) + settings: "Mapping[str, str] | None" = None + clock: "Callable[[], datetime] | None" = field(default=None, repr=False) + adaptations: AdaptationPolicy = field(default="note", kw_only=True) + + provider: str = field(default="openai-decisions", init=False) + manifest: ClassVar[ProviderManifest] = OpenAIDecisionsLM.manifest + + _inner: OpenAIDecisionsLM = field(init=False, repr=False, compare=False) + + def __post_init__(self) -> None: + self._inner = OpenAIDecisionsLM( + adaptations=self.adaptations, + api_key=self.api_key, + transport=_ForbiddenTransport(), + base_url=self.base_url, + access=self.access, + credentials_path=self.credentials_path, + settings=self.settings, + clock=self.clock, + ) + self._mirror_binding() diff --git a/lm15/providers/openai_decisions.py b/lm15/providers/openai_decisions.py new file mode 100644 index 00000000..3673eeb3 --- /dev/null +++ b/lm15/providers/openai_decisions.py @@ -0,0 +1,238 @@ +"""OpenAI Decisions: typed judgments, not text generation. + +Wire contract: https://developers.openai.com/api/docs/guides/decisions +The Responses codec supplies conversation input; decisions have their own +questions, answers, capabilities, and endpoint. No SDK dependency is needed. +""" + +from __future__ import annotations + +import json +from dataclasses import dataclass, field, fields +from typing import Any, ClassVar + +from ..access import OPENAI_DECISIONS_API +from ..adaptation import adapt +from ..errors import ProviderError, UnsupportedFeatureError +from ..features import ProviderManifest +from ..judgments import non_judgment_properties, request_judgments +from ..types import Config, DataPart, ImagePart, Message, Part, RefusalPart, Request, Response, TextPart +from .base import HttpResponse +from .common import parts_to_text, usage_from_responses +from .openai import OpenAILM + + +def _text(value: Any) -> str: + return value if isinstance(value, str) else json.dumps(value, ensure_ascii=False, allow_nan=False) + + +@dataclass +class OpenAIDecisionsLM(OpenAILM): + provider: str = field(default="openai-decisions", init=False) + manifest: ClassVar[ProviderManifest] = OPENAI_DECISIONS_API + + def __post_init__(self): + self._compat_base = None + super().__post_init__() + + def _refuse(self, feature: str, reason: str) -> UnsupportedFeatureError: + return UnsupportedFeatureError(f"OpenAI Decisions: {reason}", provider=self.provider, feature=feature) + + def _questions(self, request: Request) -> list[dict[str, Any]]: + found = request_judgments(request) + fmt = request.config.response_format or {} + if not found or non_judgment_properties(fmt.get("schema"), found): + raise self._refuse("response_format", "declare only boolean, choice, or ordered-score judgments") + if len(found) > 400: + raise self._refuse("questions", "at most 400 questions are supported") + overrides = (request.config.extensions or {}).get("questions", {}) + if not isinstance(overrides, dict) or overrides.keys() - found.keys(): + raise self._refuse("questions", "question overrides must name declared judgments") + questions = [] + for name, judgment in found.items(): + if judgment.kind == "box": + raise self._refuse("response_format", "bounding boxes are not supported") + kind = {"boolean": "predicate", "choice": "choice", "ordered": "score"}[judgment.kind] + override = overrides.get(name, {}) + if not isinstance(override, dict) or override.keys() - {"type", "instructions", "criteria"}: + raise self._refuse("questions", f"invalid question override for {name!r}") + if override.get("type", kind) not in ({"noul", "predicate"} if kind == "predicate" else {kind}): + raise self._refuse("questions", f"override changes the answer type for {name!r}") + instruction = override.get("instructions", judgment.instruction or name) + question: dict[str, Any] = {"name": name, "type": kind, "instructions": _text(instruction)} + criteria: Any = override.get("criteria") + if kind == "predicate": + if criteria is not None: + question["instructions"] = _text({"instructions": instruction, "criteria": criteria}) + elif kind == "choice": + if not 2 <= len(judgment.keys) <= 255: + raise self._refuse("questions", "choices require 2–255 options") + criteria = criteria if criteria is not None else judgment.descriptions + if not isinstance(criteria, dict) or set(criteria) != set(judgment.keys): + raise self._refuse("questions", f"choice criteria must preserve the keys of {name!r}") + question["choices"] = [ + {"value": key, **({"description": _text(criteria[key])} if criteria[key] is not None else {})} + for key in judgment.keys + ] + else: + criteria = ( + criteria if criteria is not None else [judgment.descriptions.get(k) or k for k in judgment.keys] + ) + if ( + not isinstance(criteria, list) + or len(criteria) != len(judgment.keys) + or not 2 <= len(criteria) <= 10 + ): + raise self._refuse("questions", "score criteria must preserve the declared 2–10 levels") + question["levels"] = [ + {"label": key, "description": _text(desc)} for key, desc in zip(judgment.keys, criteria) + ] + questions.append(question) + return questions + + def _payload(self, request: Request, stream: bool) -> dict[str, Any]: + if stream: + raise self._refuse("stream", "streaming is not supported") + if request.tools: + raise self._refuse("tools", "decisions do not invoke tools") + cfg = request.config + defaults = Config() + for item in fields(cfg): + value = getattr(cfg, item.name) + if item.name in {"response_format", "extensions", "probabilities", "user_id"} or value == getattr(defaults, item.name): + continue + if item.name in {"temperature", "top_p", "top_k", "seed", "frequency_penalty", "presence_penalty"}: + adapt(f"config.{item.name}", "dropped", "MAP-13: decisions score fixed alternatives rather than sampling text", + asked=value, provider=self.provider) + else: + raise self._refuse(f"config.{item.name}", f"MAP-13: {item.name} cannot be mapped safely to Decisions") + extensions = cfg.extensions or {} + extra = extensions.keys() - {"questions", "prompt_cache_key", "prompt_cache_options", "safety_identifier"} + if extra: + raise self._refuse("config.extensions", f"unsupported options: {sorted(extra)}") + # Admit only content this endpoint can read. Never let the broader + # Responses codec silently drop or send unsupported media. + for message in request.messages: + for part in message.parts: + allowed = ( + (TextPart, DataPart, ImagePart) + if message.role == "user" + else (TextPart, RefusalPart) + if message.role == "assistant" + else (TextPart,) + ) + if not isinstance(part, allowed) or message.role == "tool": + raise self._refuse("messages", "use text/JSON/images in user messages and text in other roles") + inputs = self._build_input(request.messages, self._compat(request)) + if request.system: + system = request.system if isinstance(request.system, str) else parts_to_text(request.system) + inputs.insert(0, {"role": "developer", "content": system}) + payload: dict[str, Any] = {"model": request.model, "input": inputs, "questions": self._questions(request)} + if cfg.user_id is not None: + payload["safety_identifier"] = cfg.user_id + payload.update({key: value for key, value in extensions.items() if key != "questions"}) + return payload + + def build_request(self, request: Request, stream: bool): + request = self._wire_request(request) + return self._emit( + method="POST", + url=f"{self.base_url.rstrip('/')}/decisions", + endpoint="decisions", + model=request.model, + headers=self._headers(), + payload=self._payload(request, stream), + ) + + def parse_response(self, request: Request, response: HttpResponse) -> Response: + request = self._wire_request(request) + data = response.json() + questions = self._questions(request) + + def invalid(detail): + return ProviderError( + f"Malformed OpenAI Decisions reply: {detail}", + provider=self.provider, + status=response.status, + request_id=response.header("x-request-id"), + ) + + def probability(value): + if isinstance(value, bool) or not isinstance(value, (int, float)) or not 0 <= value <= 1: + raise invalid("probabilities must be finite numbers in [0, 1]") + return float(value) + + if not isinstance(data, dict) or not isinstance(data.get("answers"), list): + raise invalid("expected an answers array") + raw_answers = data["answers"] + if len(raw_answers) != len(questions): + raise invalid("answer count differs from question count") + values, distributions, evidence, refusals = {}, {}, {}, [] + for question, answer in zip(questions, raw_answers): + name, kind = question["name"], question["type"] + if not isinstance(answer, dict) or answer.get("name") != name: + raise invalid("answers must match question names and order") + if answer.get("type") == "refusal": + refusals.append(name) + continue + if answer.get("type") != kind: + raise invalid(f"wrong answer type for {name!r}") + if kind == "predicate": + p = probability(answer.get("probability")) + values[name] = p >= 0.5 + distributions[name] = {"true": p, "false": 1 - p} + evidence[name] = {"noul": p} + continue + raw = answer.get("probabilities") + expected = ( + [x["value"] for x in question["choices"]] if kind == "choice" else list(range(len(question["levels"]))) + ) + if not isinstance(raw, list) or len(raw) != len(expected): + raise invalid(f"incomplete distribution for {name!r}") + probs = {} + for entry in raw: + if not isinstance(entry, dict): + raise invalid("expected probability objects") + key = entry.get("value") + if type(key) is not type(expected[0]) or key not in expected or str(key) in probs: + raise invalid(f"invalid or duplicate distribution key for {name!r}") + if kind == "score" and entry.get("label") != question["levels"][key]["label"]: + raise invalid(f"invalid score label for {name!r}") + probs[str(key)] = probability(entry.get("probability")) + confidence = probability(answer.get("confidence")) + if kind == "choice": + value = answer.get("choice") + if type(value) is not str or value not in expected: + raise invalid(f"invalid selected choice for {name!r}") + else: + score = answer.get("score") + if ( + isinstance(score, bool) + or not isinstance(score, (int, float)) + or not 0 <= score <= len(expected) - 1 + ): + raise invalid(f"invalid score for {name!r}") + value = int(max(probs, key=lambda key: probs[key])) + values[name], distributions[name] = value, probs + evidence[name] = {"probabilities": probs, "confidence": confidence} + try: + parts: list[Part] = [ + DataPart( + value=values, + probabilities=distributions or None, + method="provider_classification" if distributions else None, + ) + ] + if refusals: + parts.append(RefusalPart(text="Decisions refused: " + ", ".join(refusals))) + provider_data: dict[str, Any] = {self.provider: data, "decisions": {"answers": evidence, "refusals": refusals}} + return Response( + id=response.header("x-request-id"), + model=data.get("model", request.model), + message=Message(role="assistant", parts=tuple(parts)), + finish_reason="content_filter" if refusals else "stop", + usage=usage_from_responses(data.get("usage")), + provider_data=provider_data, + ) + except (TypeError, ValueError, OverflowError) as error: + raise invalid(str(error)) from error diff --git a/lm15/registry.py b/lm15/registry.py index 568a6147..af7843cf 100644 --- a/lm15/registry.py +++ b/lm15/registry.py @@ -75,6 +75,8 @@ class manifest. OpenAICodexLM, OpenAILM, TypeSafeLM, + OpenAIDecisionsLM, + AsyncOpenAIDecisionsLM, XaiLM, ) @@ -89,7 +91,7 @@ class manifest. # The wire formats lm15 speaks. A dialect is a class; a provider is a # dialect plus an access policy (plus a compat preset for the chat dialect). -Dialect = Literal["openai-responses", "openai-chat", "anthropic", "gemini", "typesafe"] +Dialect = Literal["openai-responses", "openai-chat", "anthropic", "gemini", "typesafe", "openai-decisions"] # A compat value: a preset name for a registry entry (validated against # the dialect's table below), or the object itself for a declared entry. @@ -396,6 +398,11 @@ def _hosted( console_url="https://platform.openai.com/api-keys", note="OpenAI Responses API", ), + _adapter_owned( + "openai-decisions", "openai-decisions", OpenAIDecisionsLM, AsyncOpenAIDecisionsLM, + console_url="https://platform.openai.com/api-keys", + note="OpenAI Decisions API: predicates, choices, and scores; no text generation", + ), _adapter_owned( "openai-chat", "openai-chat", OpenAIChatLM, AsyncOpenAIChatLM, console_url="https://platform.openai.com/api-keys", diff --git a/tests/test_adaptations.py b/tests/test_adaptations.py index 5f17acde..1a3acdf0 100644 --- a/tests/test_adaptations.py +++ b/tests/test_adaptations.py @@ -336,9 +336,9 @@ def __getitem__(self, key): router = LMRouter(RouterConfig(env={})) for pid in PROVIDERS: model = {"deepseek-anthropic": "deepseek-v4-flash", "moonshotai-anthropic": "kimi-k3"}.get(pid, "m") - # typesafe answers declared judgments only (MAP-14): a plain text + # Native decision providers answer declared judgments only: a plain text # request is a refusal there, not a credential question. - config = Config(response_format=judgments(ok=yes_no("Is it fine?"))) if pid == "typesafe" else Config() + config = Config(response_format=judgments(ok=yes_no("Is it fine?"))) if pid in {"typesafe", "openai-decisions"} else Config() router.plan(Request(model=f"{pid}:{model}", messages=(Message.user("hi"),), config=config)) assert router._lms == {} # planning LMs are never cached diff --git a/tests/test_openai_decisions.py b/tests/test_openai_decisions.py new file mode 100644 index 00000000..4402aaaa --- /dev/null +++ b/tests/test_openai_decisions.py @@ -0,0 +1,191 @@ +import copy +import json +from dataclasses import replace + +import pytest + +from lm15 import ( + AsyncLMRouter, + Config, + DataPart, + ImagePart, + LMRouter, + Message, + OpenAIDecisionsLM, + ProviderError, + Request, + RouterConfig, + UnsupportedFeatureError, + bounding_box, + choice, + judgments, + score, + yes_no, +) +from lm15.testing import FakeResponse, FakeTransport + +REQUEST = Request( + model="openai-decisions:gpt-6-luna", + messages=(Message.user(DataPart(value={"ticket": "Charged twice"})),), + config=Config( + response_format=judgments( + urgent=yes_no("Is it urgent?"), + department=choice("Which department?", ["billing", "other"]), + severity=score("How severe?", ["low", "medium", "high"]), + ) + ), +) +BODY = { + "model": "gpt-6-luna", + "answers": [ + {"name": "urgent", "type": "predicate", "probability": 0.8}, + { + "name": "department", + "type": "choice", + "choice": "billing", + "confidence": 0.9, + "probabilities": [{"value": "billing", "probability": 0.9}, {"value": "other", "probability": 0.1}], + }, + { + "name": "severity", + "type": "score", + "score": 1.6, + "confidence": 0.7, + "probabilities": [ + {"value": 0, "label": "0", "probability": 0.1}, + {"value": 1, "label": "1", "probability": 0.2}, + {"value": 2, "label": "2", "probability": 0.7}, + ], + }, + ], + "usage": { + "input_tokens": 100, + "output_tokens": 0, + "total_tokens": 100, + "input_tokens_details": {"cached_tokens": 20, "cache_write_tokens": 10}, + }, +} + + +def transport(body=BODY, status=200): + return FakeTransport( + [FakeResponse(status=status, body=json.dumps(body).encode(), headers=[("x-request-id", "req-test")])] + ) + + +def test_router_wire_and_all_answer_types(): + wire = transport() + router = LMRouter(RouterConfig(api_keys={"openai-decisions": "test-key"}, transport=wire)) + response = router.complete(REQUEST) + sent = wire.requests[0] + assert sent.url == "https://api.openai.com/v1/decisions" + payload = json.loads(sent.body) + assert payload["model"] == "gpt-6-luna" + assert payload["questions"][1]["choices"] == [{"value": "billing"}, {"value": "other"}] + assert payload["questions"][2]["levels"][1] == {"label": "1", "description": "medium"} + assert response.data == {"urgent": True, "department": "billing", "severity": 2} + assert response.expected("severity") == pytest.approx(1.6) + assert response.id == "req-test" + assert response.usage.cache_read_tokens == 20 + assert response.usage.cache_write_tokens == 10 + assert response.usage.output_tokens == 0 + assert response.provider_data["openai-decisions"] == BODY + assert response.provider_data["decisions"]["answers"]["urgent"] == {"noul": 0.8} + + +def test_structured_overrides_and_image_input(): + request = replace( + REQUEST, + messages=(Message.user(["Inspect", ImagePart(media_type="image/png", url="https://example.com/image.png")]),), + config=replace( + REQUEST.config, + extensions={ + "questions": { + "urgent": {"type": "noul", "instructions": {"rule": "Urgent?"}, "criteria": {"true": "today"}}, + "department": {"criteria": {"billing": {"examples": ["refund"]}, "other": None}}, + } + }, + ), + ) + payload = json.loads(OpenAIDecisionsLM(api_key="test").build_request(request, False).body) + assert payload["input"][0]["content"][1]["image_url"] == "https://example.com/image.png" + assert json.loads(payload["questions"][0]["instructions"])["criteria"] == {"true": "today"} + assert json.loads(payload["questions"][1]["choices"][0]["description"]) == {"examples": ["refund"]} + + +@pytest.mark.parametrize("field,value", [("max_tokens", 10), ("store", False)]) +def test_rejects_generation_settings_before_transport(field, value): + wire = transport() + lm = OpenAIDecisionsLM(api_key="test", transport=wire) + with pytest.raises(UnsupportedFeatureError): + lm.complete(replace(REQUEST, config=replace(REQUEST.config, **{field: value}))) + assert not wire.requests + + +def test_rejects_boxes_and_streaming(): + lm = OpenAIDecisionsLM(api_key="test") + with pytest.raises(UnsupportedFeatureError, match="bounding boxes"): + lm.build_request(replace(REQUEST, config=Config(response_format=judgments(box=bounding_box("Locate")))), False) + with pytest.raises(UnsupportedFeatureError, match="streaming"): + lm.build_request(REQUEST, True) + + +@pytest.mark.parametrize( + "mutation", + [ + lambda b: b["answers"].pop(), + lambda b: b["answers"][0].update(name="wrong"), + lambda b: b["answers"][0].update(probability=float("nan")), + lambda b: b["answers"][0].update(probability=True), + lambda b: b["answers"][1]["probabilities"][1].update(value="billing"), + lambda b: b["answers"][1].update(confidence=2), + lambda b: b["answers"][2]["probabilities"][0].update(value=False), + lambda b: b["answers"][2]["probabilities"][0].update(label="wrong"), + lambda b: b["answers"][2].update(score=10**400), + ], +) +def test_rejects_malformed_answers(mutation): + body = copy.deepcopy(BODY) + mutation(body) + with pytest.raises(ProviderError, match="Malformed OpenAI Decisions"): + OpenAIDecisionsLM(api_key="test", transport=transport(body)).complete(REQUEST) + + +def test_refusal_preserves_other_answers_and_usage(): + body = copy.deepcopy(BODY) + body["answers"][0] = {"name": "urgent", "type": "refusal"} + response = OpenAIDecisionsLM(api_key="test", transport=transport(body)).complete(REQUEST) + assert response.finish_reason == "content_filter" + assert "urgent" not in response.data + assert response.data["department"] == "billing" + assert response.usage.input_tokens == 100 + assert response.provider_data["decisions"]["refusals"] == ["urgent"] + + +@pytest.mark.asyncio +async def test_async_router_uses_same_codec(): + from tests.test_router import FakeAsyncResponse, FakeAsyncTransport + + wire = FakeAsyncTransport([FakeAsyncResponse(200, json.dumps(BODY).encode())]) + router = AsyncLMRouter(RouterConfig(api_keys={"openai-decisions": "test"}, transport=wire)) + response = await router.complete(REQUEST) + assert response.data["department"] == "billing" + + +@pytest.mark.parametrize("policy", ["note", "silent", "refuse"]) +def test_sampling_hints_follow_adaptation_policy(policy): + wire = transport() + lm = OpenAIDecisionsLM(api_key="test", transport=wire, adaptations=policy) + request = replace(REQUEST, config=replace(REQUEST.config, temperature=0.2)) + if policy == "refuse": + with pytest.raises(UnsupportedFeatureError, match="config.temperature"): + lm.complete(request) + assert not wire.requests + else: + response = lm.complete(request) + assert "temperature" not in json.loads(wire.requests[0].body) + assert response.data["urgent"] is True + if policy == "note": + assert [(a.field, a.action) for a in response.adaptations] == [("config.temperature", "dropped")] + else: + assert response.adaptations == () diff --git a/tests/test_registry.py b/tests/test_registry.py index b54bcc90..7864357c 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -39,7 +39,7 @@ def test_one_dialect_per_provider_string(self) -> None: # A provider string names ONE wire behavior; the adapter class is # the dialect, so two entries never share an id (a mapping cannot) # and every entry names a dialect the class actually speaks. - dialect_of = {"openai-responses": "OpenAI", "openai-chat": "OpenAIChat", "anthropic": "Anthropic", "gemini": "Gemini", "typesafe": "TypeSafe"} + dialect_of = {"openai-responses": "OpenAI", "openai-chat": "OpenAIChat", "anthropic": "Anthropic", "gemini": "Gemini", "typesafe": "TypeSafe", "openai-decisions": "OpenAIDecisions"} bound_class = {"openai-responses": OpenAILM, "openai-chat": OpenAIChatLM, "anthropic": AnthropicLM, "gemini": GeminiLM} for d in PROVIDERS.values(): assert d.adapter.__name__.endswith("LM")