Commit 713cd65
committed
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).1 parent 63d2647 commit 713cd65
9 files changed
Lines changed: 1083 additions & 93 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 | | |
| |||
739 | 776 | | |
740 | 777 | | |
741 | 778 | | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
742 | 815 | | |
| 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