fix(deepseek): leave malformed turns untouched when filling empty thinking blocks

This commit is contained in:
mateo-berri 2026-11-06 07:30:16 -08:00
parent 7d173332a2
commit 75d61fa297
2 changed files with 17 additions and 0 deletions

View file

@ -36,11 +36,15 @@ def _sanitized_tool(tool: object) -> object:
def _tool_turn_with_empty_thinking(message: object) -> Mapping[object, object] | None:
if not isinstance(message, dict):
return None
fields: Final = _OBJECT_FIELDS.validate_python(message)
content: Final = fields.get("content")
if not isinstance(content, list):
return None
blocks: Final = _OBJECT_LIST.validate_python(content)
if not all(isinstance(block, dict) for block in blocks):
return None
block_types: Final = frozenset(_OBJECT_FIELDS.validate_python(block).get("type") for block in blocks)
if "tool_use" not in block_types or not block_types.isdisjoint(_THINKING_BLOCK_TYPES):
return None

View file

@ -264,3 +264,16 @@ def test_deepseek_anthropic_messages_leaves_history_alone_when_thinking_disabled
request = _transform(_tool_loop_history(), {"thinking": {"type": "disabled"}})
assert request["messages"] == _tool_loop_history()
def test_deepseek_anthropic_messages_passes_malformed_turns_through_unchanged():
malformed_turn = {
"role": "assistant",
"content": [{"type": "tool_use", "id": "toolu_1", "name": "bash", "input": {"command": "ls"}}, "oops"],
}
tail = _tool_loop_history()[8:]
request = _transform(["hello", malformed_turn, *tail], {})
filled_turn = {**tail[1], "content": [{"type": "thinking", "thinking": ""}, *tail[1]["content"]]}
assert request["messages"] == ["hello", malformed_turn, tail[0], filled_turn, tail[2]]