Skip to content

Commit 30d43c0

Browse files
release: 0.1.0-alpha.54 (#212)
* chore: add UnionUnmarshaler for responses that are interfaces (#211) * fix(streaming): correctly decode assistant events * release: 0.1.0-alpha.54 --------- Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com> Co-authored-by: Robert Craigie <[email protected]>
1 parent 7daddef commit 30d43c0

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "0.1.0-alpha.53"
2+
".": "0.1.0-alpha.54"
33
}

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## 0.1.0-alpha.54 (2025-02-05)
4+
5+
Full Changelog: [v0.1.0-alpha.53...v0.1.0-alpha.54](https://github.com/openai/openai-go/compare/v0.1.0-alpha.53...v0.1.0-alpha.54)
6+
7+
### Bug Fixes
8+
9+
* **streaming:** correctly decode assistant events ([38ded46](https://github.com/openai/openai-go/commit/38ded4694480071a768eb4d2790ba4552e001506))
10+
11+
12+
### Chores
13+
14+
* add UnionUnmarshaler for responses that are interfaces ([#211](https://github.com/openai/openai-go/issues/211)) ([185d848](https://github.com/openai/openai-go/commit/185d848cd3e9efb48f7468acc06a8b78f3a7b785))
15+
316
## 0.1.0-alpha.53 (2025-02-05)
417

518
Full Changelog: [v0.1.0-alpha.52...v0.1.0-alpha.53](https://github.com/openai/openai-go/compare/v0.1.0-alpha.52...v0.1.0-alpha.53)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Or to pin the version:
2525
<!-- x-release-please-start-version -->
2626

2727
```sh
28-
go get -u 'github.com/openai/[email protected].53'
28+
go get -u 'github.com/openai/[email protected].54'
2929
```
3030

3131
<!-- x-release-please-end -->

internal/apijson/registry.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ func RegisterUnion(typ reflect.Type, discriminator string, variants ...UnionVari
2929
unionVariants[variant.Type] = typ
3030
}
3131
}
32+
33+
// Useful to wrap a union type to force it to use [apijson.UnmarshalJSON] since you cannot define an
34+
// UnmarshalJSON function on the interface itself.
35+
type UnionUnmarshaler[T any] struct {
36+
Value T
37+
}
38+
39+
func (c *UnionUnmarshaler[T]) UnmarshalJSON(buf []byte) error {
40+
return UnmarshalRoot(buf, &c.Value)
41+
}

internal/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
package internal
44

5-
const PackageVersion = "0.1.0-alpha.53" // x-release-please-version
5+
const PackageVersion = "0.1.0-alpha.54" // x-release-please-version

packages/ssestream/ssestream.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,29 @@ func (s *Stream[T]) Next() bool {
158158
continue
159159
}
160160

161-
ep := gjson.GetBytes(s.decoder.Event().Data, "error")
162-
if ep.Exists() {
163-
s.err = fmt.Errorf("received error while streaming: %s", ep.String())
164-
return false
165-
}
166-
s.err = json.Unmarshal(s.decoder.Event().Data, &s.cur)
167-
if s.err != nil {
168-
return false
161+
if s.decoder.Event().Type == "" {
162+
ep := gjson.GetBytes(s.decoder.Event().Data, "error")
163+
if ep.Exists() {
164+
s.err = fmt.Errorf("received error while streaming: %s", ep.String())
165+
return false
166+
}
167+
s.err = json.Unmarshal(s.decoder.Event().Data, &s.cur)
168+
if s.err != nil {
169+
return false
170+
}
171+
return true
172+
} else {
173+
ep := gjson.GetBytes(s.decoder.Event().Data, "error")
174+
if ep.Exists() {
175+
s.err = fmt.Errorf("received error while streaming: %s", ep.String())
176+
return false
177+
}
178+
s.err = json.Unmarshal([]byte(fmt.Sprintf(`{ "event": %q, "data": %s }`, s.decoder.Event().Type, s.decoder.Event().Data)), &s.cur)
179+
if s.err != nil {
180+
return false
181+
}
182+
return true
169183
}
170-
return true
171184
}
172185

173186
return false

0 commit comments

Comments
 (0)