Skip to content

fix(providers): handle empty assistant replies and missing tool results for strict OpenAI endpoints - #33

Open
hw0rld wants to merge 1 commit into
JailbrokenAI:mainfrom
hw0rld:fix/deepseek-empty-assistant-tool-results
Open

fix(providers): handle empty assistant replies and missing tool results for strict OpenAI endpoints#33
hw0rld wants to merge 1 commit into
JailbrokenAI:mainfrom
hw0rld:fix/deepseek-empty-assistant-tool-results

Conversation

@hw0rld

@hw0rld hw0rld commented Aug 28, 2026

Copy link
Copy Markdown

What

Fixes two HTTP 400 crashes when running against strict OpenAI-compatible
endpoints (DeepSeek). Both crashes killed live engagements and made resumed
sessions unusable.

Bug 1: empty assistant messages

When the target returns a reasoning-only reply (content empty, CoT present),
query_target / continue_target persisted an assistant message with empty
content into the target thread. DeepSeek rejects the next request with:

Invalid assistant message: content or tool_calls must be set

Fix:

  • tools/target.py _persist_thread: an empty reply no longer appends an empty
    assistant message to the thread.
  • tools/target.py _continue_target: an empty reply folds the recovered CoT
    into the message body, or uses a small marker when even CoT is missing.
  • providers/openai_provider.py _messages_to_wire: defense-in-depth — skip
    assistant messages with neither text nor tool calls (also covers resumed
    sessions that already contain poisoned history).

Bug 2: unfinished tool-call rounds on resume

A session that crashed mid-tool-execution can end with an assistant message
carrying tool_calls but no recorded tool results. Resuming and sending the
next turn makes DeepSeek reject the request with:

An assistant message with 'tool_calls' must be followed by tool messages
responding to each 'tool_call_id'. (insufficient tool messages following
tool_calls message)

Fix:

  • providers/openai_provider.py: new _complete_missing_tool_results() inserts
    placeholder tool results for any missing tool_call_id when serializing to
    the wire, so recovered sessions stay valid.

Verification

  • Real DeepSeek API: the previously-failing resumed-session request now returns
    200 OK.
  • pytest tests -k "target or continue or provider or session": 223 passed.
  • The two unrelated failures (test_tui_autocomplete resume picker,
    test_tg5_harden missing hypothesis) fail identically on main before this
    change.

Impact

Token cost is negligible (placeholders are a few tokens; empty replies now carry
the already-recovered CoT instead of nothing). No behavior change for healthy
threads.

…ts for strict OpenAI endpoints

DeepSeek rejects two message shapes that Wallbreaker could produce, crashing
engagements with HTTP 400:

1. Empty assistant messages: when the target returns a reasoning-only reply
   (no content), query_target/continue_target persisted an assistant message
   with empty content. DeepSeek rejects this with 'Invalid assistant message:
   content or tool_calls must be set'. Fix: never persist an empty assistant
   message in the target thread; when a reply is empty, fold the recovered CoT
   into the message or use a marker. Also skip empty assistant messages in the
   OpenAI wire serializer as a defense-in-depth guard.

2. Unfinished tool-call rounds on resume: a crashed session can end with an
   assistant message carrying tool_calls but no recorded tool results. DeepSeek
   rejects the next request ('An assistant message with tool_calls must be
   followed by tool messages responding to each tool_call_id'). Fix: the OpenAI
   wire serializer inserts placeholder tool results for any missing tool_call_id
   so resumed sessions stay valid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant