agharsallah commited on
Commit ·
330d790
1
Parent(s): b08315f
feat: refine structured output handling with guided decoding and mode adjustments
Browse files- docs/adr/0016-instructor-structured-output.md +22 -0
- modal/catalogue.py +5 -0
- tests/test_instructor.py +31 -3
docs/adr/0016-instructor-structured-output.md
CHANGED
|
@@ -66,6 +66,28 @@ recorded from the provider in every branch, so the conductor's
|
|
| 66 |
`pyproject.toml`. Lazy imports keep `import src.*` and `import app` working with
|
| 67 |
it not installed.
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
## Consequences
|
| 70 |
|
| 71 |
- On the live path, agent output is schema-valid and kind-constrained by
|
|
|
|
| 66 |
`pyproject.toml`. Lazy imports keep `import src.*` and `import app` working with
|
| 67 |
it not installed.
|
| 68 |
|
| 69 |
+
## Refinement: guided decoding, not tool calling (2026-06)
|
| 70 |
+
|
| 71 |
+
The first cut left Instructor on its default `Mode.TOOLS`, which encodes the
|
| 72 |
+
schema as an OpenAI **function/tool call**. That only validates on a served model
|
| 73 |
+
whose vLLM deployment has tool calling enabled with a *matching* parser. The
|
| 74 |
+
`fast` tier (`minicpm-4-1-8b`, ADR-0022 catalogue) has neither: MiniCPM4.1 emits a
|
| 75 |
+
custom `<|tool_call_start|> … <|tool_call_end|>` format for which vLLM 0.21.0 ships
|
| 76 |
+
no parser, so every structured call returned **`400 Bad Request`** (rejected at
|
| 77 |
+
request validation, ~40 ms, no generation) and degraded to the prose fallback —
|
| 78 |
+
turning the `fast` tier's fast validated-JSON path into a ~7 s prose round-trip
|
| 79 |
+
every turn, and feeding the `clean_clue` over-filter that dropped first-person
|
| 80 |
+
clues (the `spy-bex` "no usable line" failure).
|
| 81 |
+
|
| 82 |
+
`LiteLLMProvider.structured_mode` now defaults to **`json_schema`** — vLLM
|
| 83 |
+
**guided decoding** via `response_format`, which constrains output to the schema
|
| 84 |
+
*without* a tool-call parser, so it is correct for every served model regardless of
|
| 85 |
+
tool support (Gemma/Nemotron keep validating; MiniCPM now validates instead of
|
| 86 |
+
400ing). It is a per-provider field (an `instructor.Mode` member name): `json`
|
| 87 |
+
(plain `json_object` + schema-in-prompt) is the fallback if a backend rejects
|
| 88 |
+
`json_schema`, and `tools` restores the old behaviour for a model that prefers it.
|
| 89 |
+
No redeploy is needed — the change is entirely client-side on the request shape.
|
| 90 |
+
|
| 91 |
## Consequences
|
| 92 |
|
| 93 |
- On the live path, agent output is schema-valid and kind-constrained by
|
modal/catalogue.py
CHANGED
|
@@ -197,6 +197,11 @@ OPENBMB_MODELS: tuple[ModelConfig, ...] = (
|
|
| 197 |
max_model_len=32768,
|
| 198 |
trust_remote_code=True,
|
| 199 |
max_concurrent_inputs=48,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
),
|
| 201 |
ModelConfig(
|
| 202 |
name="openbmb/MiniCPM-o-4_5",
|
|
|
|
| 197 |
max_model_len=32768,
|
| 198 |
trust_remote_code=True,
|
| 199 |
max_concurrent_inputs=48,
|
| 200 |
+
# No tool_call_parser on purpose: MiniCPM4.1 emits a custom
|
| 201 |
+
# <|tool_call_start|> format vLLM 0.21.0 has no parser for, so tool-call
|
| 202 |
+
# structured output 400s here. The engine's structured path uses vLLM
|
| 203 |
+
# guided decoding (response_format json_schema) instead, which is
|
| 204 |
+
# parser-independent — see ADR-0016. Don't bolt on a mismatched parser.
|
| 205 |
),
|
| 206 |
ModelConfig(
|
| 207 |
name="openbmb/MiniCPM-o-4_5",
|
tests/test_instructor.py
CHANGED
|
@@ -105,8 +105,12 @@ class _FakeInstructorClient:
|
|
| 105 |
return result, _FakeRawCompletion(hidden_cost=self._hidden_cost)
|
| 106 |
|
| 107 |
|
| 108 |
-
def _install_fakes(monkeypatch, *, client) -> None:
|
| 109 |
-
"""Inject fake ``instructor`` (from_litellm -> client) and ``litellm`` modules.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
fake_litellm = types.ModuleType("litellm")
|
| 111 |
fake_litellm.completion = lambda **kw: None
|
| 112 |
|
|
@@ -115,8 +119,16 @@ def _install_fakes(monkeypatch, *, client) -> None:
|
|
| 115 |
|
| 116 |
fake_litellm.completion_cost = _completion_cost
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
fake_instructor = types.ModuleType("instructor")
|
| 119 |
-
fake_instructor.from_litellm =
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
monkeypatch.setitem(sys.modules, "litellm", fake_litellm)
|
| 122 |
monkeypatch.setitem(sys.modules, "instructor", fake_instructor)
|
|
@@ -156,6 +168,22 @@ class TestCompleteStructured:
|
|
| 156 |
roles = [m["role"] for m in record["messages"]]
|
| 157 |
assert roles == ["system", "user"]
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def test_error_zeroes_usage_and_reraises(self, monkeypatch):
|
| 160 |
_install_fakes(monkeypatch, client=_FakeInstructorClient(raise_exc=RuntimeError("boom")))
|
| 161 |
provider = LiteLLMProvider(model="openai/m", api_base="https://x/v1")
|
|
|
|
| 105 |
return result, _FakeRawCompletion(hidden_cost=self._hidden_cost)
|
| 106 |
|
| 107 |
|
| 108 |
+
def _install_fakes(monkeypatch, *, client, from_litellm_kw: dict | None = None) -> None:
|
| 109 |
+
"""Inject fake ``instructor`` (from_litellm -> client) and ``litellm`` modules.
|
| 110 |
+
|
| 111 |
+
*from_litellm_kw*, when given, records the kwargs ``complete_structured`` passes to
|
| 112 |
+
``instructor.from_litellm`` (e.g. the chosen ``mode``) for assertion.
|
| 113 |
+
"""
|
| 114 |
fake_litellm = types.ModuleType("litellm")
|
| 115 |
fake_litellm.completion = lambda **kw: None
|
| 116 |
|
|
|
|
| 119 |
|
| 120 |
fake_litellm.completion_cost = _completion_cost
|
| 121 |
|
| 122 |
+
def _from_litellm(completion, **kw):
|
| 123 |
+
if from_litellm_kw is not None:
|
| 124 |
+
from_litellm_kw.update(kw)
|
| 125 |
+
return client
|
| 126 |
+
|
| 127 |
fake_instructor = types.ModuleType("instructor")
|
| 128 |
+
fake_instructor.from_litellm = _from_litellm
|
| 129 |
+
# Mode is an enum on the real package; a name->value stand-in is enough for the
|
| 130 |
+
# provider's ``getattr(instructor.Mode, structured_mode.upper())`` resolution.
|
| 131 |
+
fake_instructor.Mode = types.SimpleNamespace(JSON_SCHEMA="json_schema", JSON="json", TOOLS="tools")
|
| 132 |
|
| 133 |
monkeypatch.setitem(sys.modules, "litellm", fake_litellm)
|
| 134 |
monkeypatch.setitem(sys.modules, "instructor", fake_instructor)
|
|
|
|
| 168 |
roles = [m["role"] for m in record["messages"]]
|
| 169 |
assert roles == ["system", "user"]
|
| 170 |
|
| 171 |
+
def test_defaults_to_guided_json_schema_mode(self, monkeypatch):
|
| 172 |
+
# Guided decoding, not tool calling: a model with no tool-call parser (e.g. MiniCPM)
|
| 173 |
+
# still validates instead of 400ing. The mode rides on from_litellm, not the call.
|
| 174 |
+
kw: dict = {}
|
| 175 |
+
_install_fakes(monkeypatch, client=_FakeInstructorClient(), from_litellm_kw=kw)
|
| 176 |
+
provider = LiteLLMProvider(model="openai/m", api_base="https://x/v1")
|
| 177 |
+
provider.complete_structured("echo", "x", build_output_model(["agent.spoke"]))
|
| 178 |
+
assert kw["mode"] == "json_schema"
|
| 179 |
+
|
| 180 |
+
def test_structured_mode_override_is_honored(self, monkeypatch):
|
| 181 |
+
kw: dict = {}
|
| 182 |
+
_install_fakes(monkeypatch, client=_FakeInstructorClient(), from_litellm_kw=kw)
|
| 183 |
+
provider = LiteLLMProvider(model="openai/m", api_base="https://x/v1", structured_mode="tools")
|
| 184 |
+
provider.complete_structured("echo", "x", build_output_model(["agent.spoke"]))
|
| 185 |
+
assert kw["mode"] == "tools"
|
| 186 |
+
|
| 187 |
def test_error_zeroes_usage_and_reraises(self, monkeypatch):
|
| 188 |
_install_fakes(monkeypatch, client=_FakeInstructorClient(raise_exc=RuntimeError("boom")))
|
| 189 |
provider = LiteLLMProvider(model="openai/m", api_base="https://x/v1")
|