C#C
C#5mo ago
Benny

Utf8JsonReader How to partial skip across buffers

Hi, I've been meaning to write a high performance low alloc way of reading an OpenAI chat/completions request body to scan for the model property. In many cases it appears at the start of a request body, before any prompts. So there is no point in reading the entire json document as prompts can have up to 1 million chars.

I came up with this: https://gist.github.com/bbartels/b37008a1bb6b2e0f6b462920b2c85140

And while it works, it only does if the document fits into the buffer. I waote a test where a property had a significant amount of data, and .Skip would throw.

I've attempted to use TrySkip, but when that fails it resets the state of the reader back to before the skip. Is there a way to attempt a skip, but keep the reader state. Such that you can continue on the next chunk of the buffer?
Gist
GitHub Gist: instantly share code, notes, and snippets.
Was this page helpful?