Commit b0228e1
committed
gracefully fall back when model chat template doesn't support tools
Models like gemma3 use Jinja chat templates that only support alternating
user/assistant roles and cannot handle the tool/assistant(tool_calls) message
roles introduced by the agentic web-search loop.
When a 500 response containing "Jinja" is detected, the client now resets the
message history to the original user prompt, clears the tool schemas, and
retries — allowing the model to answer from training data rather than erroring
out. This covers two failure modes:
1. The model rejects the request immediately because the tools parameter
causes its template to inject an incompatible system message.
2. The model successfully emits a tool_call but then fails on the follow-up
request because it can't process the resulting "tool" role message.
Adds three unit tests covering the fallback paths and the non-Jinja error case.
Signed-off-by: Eric Curtin <eric.curtin@docker.com>1 parent 8babe24 commit b0228e1
5 files changed
Lines changed: 666 additions & 141 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
27 | 37 | | |
28 | 38 | | |
29 | 39 | | |
| |||
632 | 642 | | |
633 | 643 | | |
634 | 644 | | |
| 645 | + | |
| 646 | + | |
635 | 647 | | |
636 | 648 | | |
637 | 649 | | |
638 | 650 | | |
639 | | - | |
| 651 | + | |
640 | 652 | | |
641 | 653 | | |
642 | 654 | | |
| |||
655 | 667 | | |
656 | 668 | | |
657 | 669 | | |
658 | | - | |
| 670 | + | |
659 | 671 | | |
660 | 672 | | |
661 | 673 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
3 | 30 | | |
4 | | - | |
5 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
6 | 35 | | |
7 | 36 | | |
8 | 37 | | |
| |||
21 | 50 | | |
22 | 51 | | |
23 | 52 | | |
| 53 | + | |
24 | 54 | | |
25 | 55 | | |
26 | 56 | | |
| |||
30 | 60 | | |
31 | 61 | | |
32 | 62 | | |
33 | | - | |
34 | | - | |
35 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
36 | 67 | | |
37 | 68 | | |
38 | | - | |
39 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
40 | 72 | | |
41 | 73 | | |
42 | 74 | | |
| |||
0 commit comments