You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes, when you call stream.Recv, you get json.Unmarshal errors, such as:
invalid character ',' after top-level value
unexpected end of JSON input
looking for beginning of value
invalid character ' ' in string escape code
This occurs because the stream.Recv implementation as of v2.12.4 is:
// Recv reads a message from the stream, returning io.EOF when// all the messages have been read.func (sStream[T]) Recv() (T, error) {
varvalueTbytes, err:=s.reader.ReadFromStream()
iferr!=nil {
returnvalue, err
}
iferr:=json.Unmarshal(bytes, &value); err!=nil {
returnvalue, err
}
returnvalue, nil
}
This occurs because the actual streamed value from reader.ReadFromStream is sometimes not properly JSON formatted, such as:
\n
truncated JSONs for long citations
Solution
Make fern generate an additional stream method like:
// Recv reads a message from the stream, returning io.EOF when// all the messages have been read.func (sStream[T]) RecvRaw() ([]byte, error) {
bytes, err:=s.reader.ReadFromStream()
iferr!=nil {
returnvalue, err
}
returnbytes, nil
}
This way the user can call stream.RecvRaw() to handle the json.Unmarshal dynamically with some sort of JSON repairing.
Thank you for your in depth research into this! I have shared with @amckinney who works on the code gen. Will see if we can fix upstream or if we should merge your fix. Appreciate you sharing this
Problem
Sometimes, when you call
stream.Recv
, you getjson.Unmarshal
errors, such as:invalid character ',' after top-level value
unexpected end of JSON input
looking for beginning of value
invalid character ' ' in string escape code
This occurs because the
stream.Recv
implementation as ofv2.12.4
is:This occurs because the actual streamed value from
reader.ReadFromStream
is sometimes not properly JSON formatted, such as:\n
Solution
Make fern generate an additional stream method like:
This way the user can call
stream.RecvRaw()
to handle thejson.Unmarshal
dynamically with some sort of JSON repairing.Update: Created a fork that does exactly this:
https://github.com/Munchpass/cohere-go
Reproduce
The errors I encountered can be reproduced with any streaming workflow with tools with the SDK. It happens relatively rarely though.
The text was updated successfully, but these errors were encountered: