feat(go): refactored formatters + added support for formatting streams #3905
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Originally, formatters were implemented as something that modifies the model response message in place which was different from how it was implemented in JS. This was done in error as this solution was not capable of handling non-JSON formats (e.g.
ModelResponse.Output()assumed that the content was JSON and since there was no formatter attached to the type, it was impossible to hook into the format-specific logic).In this PR, we introduce a new interface called
StreamingFormatHandlerwhich, despite the name, is the general format handler interface. It implementsParseOutput(*Message)andParseChunk(*ModelResponseChunk)which are used when the user callsModelResponse.Output(),ModelResponseChunk.Output(), or the convenience functionOutputFrom[T any]()without modifying content in place.To make this not a breaking change, existing formats will remain and will continue to implement
ParseMessage(*Message) *Messagebut will be marked deprecated. New formats (suffixed withv2) will be registered and implement it as a passthrough and also implementStreamingFormatHandlerwhich will be the recommended way forward.Checklist (if applicable):