pasky / claude.vim

Claude vim plugin for AI pair programming - a hacker's gateway to LLMs
MIT License
202 stars 10 forks source link

chat: output context limit reached -> roles must alternate between \"user\" and \"assistant\", but found multiple \"assistant\" roles in a row"}}", further input doesn't work #11

Open hashcacher opened 2 months ago

hashcacher commented 2 months ago

A claude chat response seemed to reach the output context limit, so I asked it to continue, and I got:

Claude: Unknown Claude protocol output: "{"type":"error","error":{"type":"invalid_request_error","message":"messages: roles must alternate between \"user\" and \"assistant\", but found multiple \"assistant\" roles in a row"}}"

Now all my message result in this response. cx results in

Error detected while processing function <SNR>20_CancelClaudeResponse:
line    5:
E906: Not an open channel
Claude response cancelled.
Press ENTER or type command to continue

The only solution I can find is to restart vim

hashcacher commented 2 months ago

Correction: if I delete the Claude Chat buffer and open a new one, it works again.

hashcacher commented 2 months ago

Thank you for this plugin, by the way!

pasky commented 2 months ago

You can also manually update whatever is at the end of the buffer to restore the alternating flow.

If you can share exactly how last few lines of the buffer look like, we can consider how to handle this better.

hashcacher commented 2 months ago

Just encountered this again. This time I was focused on another buffer while Claude was outputting. It ended up appending You: to the buffer I was browsing instead of the Claude Chat buffer. And then the Chat stopped working. Here's a direct copy paste, starting with the end of the last Claude output:

(this is the end of claude's last output)
    Is there anything else you'd like me to adjust or explain regarding these mobile-friendly changes?
 (this is me trying to send another message)
    The buttons at the bottom are cut off
(I hit c-], claude's response:)
Claude: Unknown Claude protocol output: "{"type":"error","error":{"type":"invalid_request_error","message":"Your API request included an `assistant` message in the final position, which would pre-fill the `assistant` response. When using tools, pre-filling the `assistant` response is not supported."}}"
(I add a You:, trying to restore the  flow)
   You: The buttons at the bottom are cut off
Claude: Unknown Claude protocol output: "{"type":"error","error":{"type":"invalid_request_error","message":"messages: roles must alternate between \"user\" and \"assistant\", but found multiple \"assistant\" roles in a row"}}"
pasky commented 2 months ago

Ah, interesting, so somehow "The buttons at the bottom are cut off" line is missing a "You:" prefix.

And I assume you reposted the line with the prefix rather than coming back to the previous line and adding the prefix there? So the dialog ended up with two Claude: lines in a row.

I think in the future it would be nice to do two things:

  1. don't prefix "Unknown protocol output" with "Claude:", less likely to break the alternation.
  2. check the proper alternation before sending and highlight the error in the buffer if detected
hashcacher commented 2 months ago

It seems to always happen when claude cuts itself off due to max output size. Here's another example, beginning with some cut off output from Claude:

                                  {values.map(value => (
                                    <label key={value} className="inline-flex items-center mr-4 mb

You:continue 
Claude: Unknown Claude protocol output: "{"type":"error","error":{"type":"invalid_request_error","message":"messages: final assistant content cannot end with trailing whitespace"}}"
You:continue
Claude: Unknown Claude protocol output: "{"type":"error","error":{"type":"invalid_request_error","message":"messages: roles must alternate between \"user\" and \"assistant\", but found multiple \"assistant\" roles in a row"}}"
pasky commented 2 months ago

Wow that never happenned to me, interesting!

(PR would be welcome. Also just to be clear, is the workaround to fix the output in the buffer clear?)

P.S. you may want to increase the 2048 max_tokens limit

hashcacher commented 2 months ago

Ah, interesting, so somehow "The buttons at the bottom are cut off" line is missing a "You:" prefix.

that's correct, as I had my cursor in another buffer when the output finished, so it dumped the You: there instead of the claude chat

hashcacher commented 2 months ago

Wow that never happenned to me, interesting!

(PR would be welcome. Also just to be clear, is the workaround to fix the output in the buffer clear?)

P.S. you may want to increase the 2048 max_tokens limit

Thanks for the tip about the max_tokens limit. Yeah once it starts erroring, the only fix I've found is to delete the buffer.

pasky commented 2 months ago

You don't need to delete the whole buffer, you can just edit the last lines of it as appropriate to fix the issue manually.

hashcacher commented 2 months ago

When the max token issue happens, I try to close the ` block and write You: continue but it doesn't work

   (the end of the last code claude was outputting)
               Save
                </button>

You:continue

Claude: Unknown Claude protocol output: "{"type":"error","error":{"type":"invalid_request_error","message":"messages: text content blocks must contain non-whitespace text"}}"