odysseus-dev/odysseus · error · HTTPException

session_id, original_text, and instruction are required

Error message

session_id, original_text, and instruction are required

What it means

POST /api/rewrite validated the JSON successfully but at least one of the three required fields — session_id, original_text, instruction — is missing or empty (falsy). All three are mandatory because the endpoint rewrites a specific prior message under a specific instruction.

Source

Thrown at routes/chat_routes.py:2494

    # ------------------------------------------------------------------ #
    @router.post("/api/rewrite")
    async def rewrite_message(request: Request) -> StreamingResponse:
        """Rewrite the last AI message with an instruction (shorter/simpler/etc).

        Unlike the full chat pipeline, this does NOT run the agent loop or tools.
        It just asks the LLM to rewrite the given text.
        """
        try:
            body = await request.json()
        except Exception:
            raise HTTPException(400, "Invalid JSON")

        session_id = body.get("session_id")
        original_text = body.get("original_text", "")
        instruction = body.get("instruction", "")

        if not session_id or not original_text or not instruction:
            raise HTTPException(400, "session_id, original_text, and instruction are required")

        _verify_session_owner(request, session_id)

        try:
            sess = session_manager.get_session(session_id)
        except (KeyError, SessionNotFoundError):
            raise HTTPException(404, "Session not found")

        messages = [
            {"role": "system", "content": (
                "You are rewriting a previous response. Follow the instruction exactly. "
                "Output ONLY the rewritten text — no preamble, no explanation, no meta-commentary. "
                "Preserve any formatting (markdown, code blocks, lists) from the original."
            )},
            {"role": "user", "content": (
                f"Here is the original response:\n\n{original_text}\n\n"
                f"Instruction: {instruction}"
            )},

View on GitHub (pinned to f9235ebbf1)

Solutions

  1. Include all three non-empty fields: session_id, original_text, and instruction
  2. Disable the rewrite action in the UI until an instruction is entered
  3. When wiring the button, pass the exact last assistant message text as original_text

Example fix

// before
fetch('/api/rewrite', {method:'POST', headers, body: JSON.stringify({session_id: id, instruction})})
// after
fetch('/api/rewrite', {method:'POST', headers, body: JSON.stringify({session_id: id, original_text: lastAiText, instruction: instruction.trim()})})
Defensive patterns

Strategy: validation

Validate before calling

if (!session_id || !original_text?.trim() || !instruction?.trim()) {
  showValidationError('session_id, original_text, and instruction are required'); return;
}
await postRewrite({session_id, original_text, instruction});

Type guard

function isValidRewritePayload(p: unknown): p is {session_id: string; original_text: string; instruction: string} {
  const o = p as any;
  return typeof o?.session_id === 'string' && o.session_id.length > 0
      && typeof o?.original_text === 'string' && o.original_text.length > 0
      && typeof o?.instruction === 'string' && o.instruction.trim().length > 0;
}

Prevention

When it happens

Trigger: Calling /api/rewrite with an empty instruction, no session_id, or original_text of "" (note: original_text defaults to '' so omitting it always fails this check).

Common situations: UI 'rewrite' button firing before the user typed an instruction; state bugs leaving original_text empty when the last AI message wasn't captured; API callers assuming fields are optional.

Related errors


AI-assisted analysis of odysseus-dev/odysseus@f9235ebbf1 (2026-08-14). Data as JSON: /api/errors/615448a7ef6dd0d0. Report an issue: GitHub.