UIMessageStreamError means the stream you handed to createUIMessageStreamResponse contained a part that violates the UI message stream protocol. The model call may have succeeded; the packaging failed.

import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';

const stream = createUIMessageStream({
  execute({ writer }) {
    writer.write({ type: 'text-start', id: 'x' });
    // every part must match the protocol's part types
  },
});

What to know:
1. If you only use toUIMessageStream({ stream: result.stream }), you will basically never see this. It appears when you build streams manually with createUIMessageStream or inject custom parts.
2. Common trigger: a custom part with a type string that is not in the protocol, or a part missing required fields like id.
3. Another trigger: writing parts after the stream closed, or writing a start part without its matching end part.
4. InvalidStreamPartError is the lower-level sibling for data stream parts. Same idea: the part does not match the expected shape.
5. When it fires, isolate by removing your custom writer code and confirming the plain toUIMessageStream path works. Then re-add parts one at a time.
6. Keep the stream-protocol doc page open while writing custom parts. The protocol is versioned with the SDK; v7 part types are the reference, not a blog post from v4.