Commit b678059
committed
fix(provider): preserve assistant message content when reasoning blocks present
normalizeMessages() unconditionally filtered empty text parts from all
message roles, including assistant. When Anthropic adaptive thinking
(Opus 4.6+) emits an empty text part between two reasoning blocks,
removing it shifts thinking block positions and invalidates the
cryptographic signatures, causing the API to reject with:
'thinking blocks in the latest assistant message cannot be modified'
But Anthropic also rejects empty text content blocks with 'text content
blocks must be non-empty', and the AI SDK's convertToLanguageModelPrompt
strips them, so the fix requires two layers:
1. transform.ts: normalizeMessages() skips the empty-text filter for
assistant messages that contain reasoning blocks. Messages without
reasoning continue to be filtered normally. This is a defensive
guard that handles raw ModelMessage[] scenarios.
2. message-v2.ts: when building UIMessage parts, empty text parts in
assistant messages that contain reasoning blocks are replaced with
a single space. The space preserves the structural arrangement
(reasoning block positions) without triggering Anthropic's empty
content rejection or the SDK's filter. Signatures are on the
reasoning block content, not the text between them, so a non-empty
placeholder keeps the arrangement valid.
Closes #167481 parent 266e965 commit b678059
4 files changed
Lines changed: 169 additions & 12 deletions
File tree
- packages/opencode
- src
- provider
- session
- test
- provider
- session
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
| |||
60 | 63 | | |
61 | 64 | | |
62 | 65 | | |
| 66 | + | |
63 | 67 | | |
64 | 68 | | |
65 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
825 | 825 | | |
826 | 826 | | |
827 | 827 | | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
828 | 836 | | |
829 | | - | |
| 837 | + | |
| 838 | + | |
830 | 839 | | |
831 | 840 | | |
832 | | - | |
| 841 | + | |
833 | 842 | | |
834 | 843 | | |
| 844 | + | |
835 | 845 | | |
836 | 846 | | |
837 | 847 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1148 | 1148 | | |
1149 | 1149 | | |
1150 | 1150 | | |
1151 | | - | |
| 1151 | + | |
1152 | 1152 | | |
1153 | 1153 | | |
1154 | 1154 | | |
| |||
1162 | 1162 | | |
1163 | 1163 | | |
1164 | 1164 | | |
| 1165 | + | |
| 1166 | + | |
1165 | 1167 | | |
1166 | | - | |
1167 | | - | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
1168 | 1172 | | |
1169 | 1173 | | |
1170 | | - | |
| 1174 | + | |
1171 | 1175 | | |
1172 | 1176 | | |
1173 | 1177 | | |
| |||
1182 | 1186 | | |
1183 | 1187 | | |
1184 | 1188 | | |
1185 | | - | |
| 1189 | + | |
| 1190 | + | |
1186 | 1191 | | |
1187 | | - | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
1188 | 1237 | | |
1189 | 1238 | | |
1190 | 1239 | | |
| |||
1210 | 1259 | | |
1211 | 1260 | | |
1212 | 1261 | | |
1213 | | - | |
| 1262 | + | |
1214 | 1263 | | |
1215 | 1264 | | |
1216 | 1265 | | |
| |||
1224 | 1273 | | |
1225 | 1274 | | |
1226 | 1275 | | |
| 1276 | + | |
1227 | 1277 | | |
1228 | | - | |
| 1278 | + | |
1229 | 1279 | | |
1230 | | - | |
| 1280 | + | |
| 1281 | + | |
1231 | 1282 | | |
1232 | 1283 | | |
1233 | 1284 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
947 | 947 | | |
948 | 948 | | |
949 | 949 | | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
950 | 1042 | | |
951 | 1043 | | |
952 | 1044 | | |
| |||
0 commit comments