Skip to content

Commit

Permalink
outputparser: use strings.Cut in structured parser (#997)
Browse files Browse the repository at this point in the history
outputparser: replace strings.Split with strings.Cut in structured parser
  • Loading branch information
anuraaga authored Sep 13, 2024
1 parent 0cab9df commit c362f8a
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions outputparser/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@ var _ schema.OutputParser[any] = Structured{}
func (p Structured) parse(text string) (map[string]string, error) {
// Remove the ```json that should be at the start of the text, and the ```
// that should be at the end of the text.
withoutJSONStart := strings.Split(text, "```json")
if !(len(withoutJSONStart) > 1) {
_, withoutJSONStart, ok := strings.Cut(text, "```json")
if !ok {
return nil, ParseError{Text: text, Reason: "no ```json at start of output"}
}

withoutJSONEnd := strings.Split(withoutJSONStart[1], "```")
if len(withoutJSONEnd) < 1 {
jsonString, _, ok := strings.Cut(withoutJSONStart, "```")
if !ok {
return nil, ParseError{Text: text, Reason: "no ``` at end of output"}
}

jsonString := withoutJSONEnd[0]

var parsed map[string]string
err := json.Unmarshal([]byte(jsonString), &parsed)
if err != nil {
Expand Down

0 comments on commit c362f8a

Please sign in to comment.