Commit e7d01cc
fix(llm): stabilize JSON-object generation (force tool_choice + native response_format + robust planner parsing) (#78)
* fix(llm): stabilize JSON-object generation
Users reported unstable JSON-object generation. Two root causes:
1. Structured output never used provider-native guarantees: the synthetic
`emit_<schema>` tool was only *offered* (no `tool_choice`), and Strict/Json
modes collapsed to Tool universally — so the model could emit prose or
malformed args ("parse-and-pray").
2. The planner / pre-analysis JSON path used a naive first-`{`/last-`}` slice
with no fence handling and no repair, hard-erroring on fenced/prosey output.
Fix (Tier 1 + Tier 2):
- LlmClient: additive `native_structured_support()`, `complete_structured()`,
`complete_streaming_structured()` with default impls (non-breaking — existing
clients/mocks keep working).
- structured.rs: capability-aware `resolve_mode` + `StructuredDirective`. Force
`tool_choice` (Tool/Auto), and request native `response_format`
(Strict→json_schema+strict, Json→json_object) on capable providers, falling
back to forced Tool mode otherwise.
- anthropic/openai/zhipu: honor the directive (Anthropic forced tool_choice;
OpenAI tool_choice + response_format; Zhipu delegates to its inner client).
- llm_planner: reuse the robust shared extractor + add one repair retry in
`pre_analyze`.
- generate_object: stop pre-collapsing Strict/Json to Tool (engine resolves it).
Tests:
- Deep adversarial unit tests: capability/directive routing, provider
wire-format (tool_choice/response_format), adversarial JSON extraction,
planner fence/prose/brace-in-string + repair-retry. 1811 lib tests green,
fmt + clippy clean.
- New `#[ignore]` real-LLM integration test (tests/test_structured_json_real_llm.rs).
Validated end-to-end against gpt-4o via the real gateway in .a3s/config.acl:
forced tool_choice 5/5 stable (0 repairs), json_object ok, pre_analyze ok.
Notes:
- Strict json_schema is opt-in only; Auto/Tool never send response_format.
- Follow-up: override complete_streaming_structured on providers so the
streaming structured path also forces the directive (today it uses the
non-forcing default).
* fix(llm): force structured directive on the streaming path too
Follow-up to the blocking-path fix: providers now override
`complete_streaming_structured` so streaming structured generation also forces
`tool_choice` / sets native `response_format`, instead of falling back to the
non-forcing default.
- anthropic/openai: extract `send_streaming` from `complete_streaming`; the
trait methods become thin wrappers, and `complete_streaming_structured`
applies the directive before executing. The large streaming parsers are
unchanged.
- zhipu: already delegates to its inner client (no change).
- tests: RecordingClient records the streaming directive; new unit test asserts
`generate_streaming` forces the tool; new `#[ignore]` real-LLM streaming case.
Validated against gpt-4o: streaming forced tool_choice -> 8 partials, valid
object (5/5 integration cases pass).
---------
Co-authored-by: Claude <claude@anthropic.com>1 parent 63d2647 commit e7d01cc
9 files changed
Lines changed: 1254 additions & 123 deletions
File tree
- core
- src
- llm
- planning
- tools/builtin
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
152 | 153 | | |
153 | 154 | | |
154 | 155 | | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
163 | 172 | | |
164 | 173 | | |
165 | | - | |
166 | 174 | | |
167 | 175 | | |
168 | 176 | | |
| |||
259 | 267 | | |
260 | 268 | | |
261 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
262 | 299 | | |
263 | 300 | | |
264 | 301 | | |
265 | 302 | | |
266 | 303 | | |
267 | 304 | | |
268 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
269 | 331 | | |
270 | 332 | | |
271 | 333 | | |
272 | | - | |
273 | 334 | | |
274 | 335 | | |
275 | 336 | | |
| |||
739 | 800 | | |
740 | 801 | | |
741 | 802 | | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
742 | 839 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
51 | 89 | | |
52 | 90 | | |
53 | 91 | | |
| |||
0 commit comments