Skip to content

release: 0.1.0-beta.1 #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.67"
".": "0.1.0-beta.1"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.1.0-beta.1 (2025-03-22)

Full Changelog: [v0.1.0-alpha.67...v0.1.0-beta.1](https://github.com/openai/openai-go/compare/v0.1.0-alpha.67...v0.1.0-beta.1)

### Chores

* **docs:** clarify breaking changes ([#306](https://github.com/openai/openai-go/issues/306)) ([db4bd1f](https://github.com/openai/openai-go/commit/db4bd1f5304aa523a6b62da6e2571487d4248518))

## 0.1.0-alpha.67 (2025-03-21)

Full Changelog: [v0.1.0-alpha.66...v0.1.0-alpha.67](https://github.com/openai/openai-go/compare/v0.1.0-alpha.66...v0.1.0-alpha.67)
Expand Down
110 changes: 72 additions & 38 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

<a href="https://pkg.go.dev/github.com/openai/openai-go"><img src="https://pkg.go.dev/badge/github.com/openai/openai-go.svg" alt="Go Reference"></a>

This SDK includes breaking changes from the previous version to improve the ergonomics of constructing parameters and accessing responses.
This SDK includes breaking changes to improve the ergonomics of constructing parameters and accessing responses.

To reduce verbosity, the `openai.F(...)` and `param.Field[T]` have been removed.
All calls to `openai.F(...)` can be deleted.

The SDK now uses the <code>\`json:"...,omitzero"\`</code> struct tag to omit fields. Nested structs, arrays and maps
can be declared like normal.

The old SDK used interfaces for unions in requests, which required
a type assertion to access variants and fields. The new design uses
structs with a field for each variant, wherein only one field can be set.
These struct unions also expose 'Get' methods to access and mutate subfields
which may be shared by multiple variants.

# Request parameters

## Required primitives parameters serialize their zero values (`string`, `int64`, etc.)

> [!CAUTION] > **This change can cause new behavior in existing code, without compiler warnings.**
> [!CAUTION]
>
> **This change can cause new behavior in existing code, without compiler warnings.**

While migrating, ensure that all required fields are explicitly set. A required primitive
field `Age` will use the <code>\`json:"age,required"\`</code> struct tag without `omitzero`.

If a required primitive field is not set, the zero value will be serialized.
This was not the case in with `param.Field[T]`.

```diff
type FooParams struct {
- Age param.Field[int64] `json:"age,required"`
- Name param.Field[string] `json:"name"`
+ Age int64 `json:"age,required"`
+ Age int64 `json:"age,required"` // <== Notice no omitzero
+ Name param.Opt[string] `json:"name,omitzero"`
}
```
Expand Down Expand Up @@ -48,43 +68,24 @@ _ = FooParams{
</tr>
</table>

The required field `"age"` is now present as `0`. Required primitive fields without the <code>\`json:"...,omitzero"\`</code> struct tag
The required field `"age"` is now present as `0`. Fields without the <code>\`json:"...,omitzero"\`</code> struct tag
are always serialized, including their zero values.

## Transition from `param.Field[T]` to `omitzero`

The new SDK uses <a href="https://pkg.go.dev/encoding/json#Marshal"><code>\`json:"...,omitzero"\`</code> semantics</a> from Go 1.24+ for JSON encoding[^1].

`omitzero` is used for structs, slices, maps, string enums, and optional primitive types wrapped in `param.Opt[T]` (e.g. `param.Opt[string]`).

**Fields of a request struct:**

```diff
type FooParams struct {
- RequiredString param.Field[string] `json:"required_string,required"`
+ RequiredString string `json:"required_string,required"`

- OptionalString param.Field[string] `json:"optional_string"`
+ OptionalString param.Opt[string] `json:"optional_string,omitzero"`

- Array param.Field[[]BarParam] `json"array"`
+ Array []BarParam `json"array,omitzero"`
The `openai.F(...)` function and `param.Field[T]` type are no longer present in the new SDK.

- Map param.Field[map[string]BarParam] `json"map"`
+ Map map[string]BarParam `json"map,omitzero"`
To represent omitted fields, the SDK uses <a href="https://pkg.go.dev/encoding/json#Marshal"><code>\`json:"...,omitzero"\`</code> semantics</a> from Go 1.24+ for JSON encoding[^1]. `omitzero` always omits fields
with zero values.

- RequiredObject param.Field[BarParam] `json:"required_object,required"`
+ RequiredObject BarParam `json:"required_object,omitzero,required"`
In all cases other than optional primitives, `openai.F()` can simply be removed.
For optional primitive types, such as `param.Opt[string]`, you can use `openai.String(string)` to construct the value.
Similar functions exist for other primitive types like `openai.Int(int)`, `openai.Bool(bool)`, etc.

- OptionalObject param.Field[BarParam] `json:"optional_object"`
+ OptionalObject BarParam `json:"optional_object,omitzero"`
`omitzero` is used for fields whose type is either a struct, slice, map, string enum,
or wrapped optional primitive (e.g. `param.Opt[T]`). Required primitive fields don't use `omitzero`.

- StringEnum param.Field[BazEnum] `json:"string_enum"`
+ StringEnum BazEnum `json:"string_enum,omitzero"`
}
```

**Previous vs New SDK: Constructing a request**
**Example User Code: Constructing a request**

```diff
foo = FooParams{
Expand Down Expand Up @@ -112,12 +113,36 @@ foo = FooParams{
}
```

`param.Opt[string]` can be constructed with `openai.String(string)`. Similar functions exist for other primitive
types like `openai.Int(int)`, `openai.Bool(bool)`, etc.
**Internal SDK Code: Fields of a request struct:**

```diff
type FooParams struct {
- RequiredString param.Field[string] `json:"required_string,required"`
+ RequiredString string `json:"required_string,required"`

- OptionalString param.Field[string] `json:"optional_string"`
+ OptionalString param.Opt[string] `json:"optional_string,omitzero"`

- Array param.Field[[]BarParam] `json"array"`
+ Array []BarParam `json"array,omitzero"`

- Map param.Field[map[string]BarParam] `json"map"`
+ Map map[string]BarParam `json"map,omitzero"`

- RequiredObject param.Field[BarParam] `json:"required_object,required"`
+ RequiredObject BarParam `json:"required_object,omitzero,required"`

- OptionalObject param.Field[BarParam] `json:"optional_object"`
+ OptionalObject BarParam `json:"optional_object,omitzero"`

- StringEnum param.Field[BazEnum] `json:"string_enum"`
+ StringEnum BazEnum `json:"string_enum,omitzero"`
}
```

## Request Unions: Removing interfaces and moving to structs

For a type `AnimalUnionParam` which could be either a `string | CatParam | DogParam`.
For a type `AnimalUnionParam` which could be either a `CatParam | DogParam`.

<table>
<tr><th>Previous</th> <th>New</th></tr>
Expand Down Expand Up @@ -237,14 +262,23 @@ The `.IsNull()` method has been changed to `.IsPresent()` to better reflect its
```diff
- if !resp.Foo.JSON.Bar.IsNull() {
+ if resp.Foo.JSON.Bar.IsPresent() {
println("bar is present:", resp.Foo.Bar)
}
println("bar is present:", resp.Foo.Bar)
}
```

| Previous | New | Returns true for values |
| -------------- | ------------------- | ----------------------- |
| `.IsNull()` | `!.IsPresent()` | `null` or Omitted |
| `.IsMissing()` | `.Raw() == ""` | Omitted |
| `.Invalid()` | `.IsExplicitNull()` | `null` |
| | `.IsExplicitNull()` | `null` |

## Checking Raw JSON of a response

The `.RawJSON()` method has moved to the parent of the `.JSON` property.

```diff
- resp.Foo.JSON.RawJSON()
+ resp.Foo.RawJSON()
```

[^1]: The SDK doesn't require Go 1.24, despite supporting the `omitzero` feature
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
The OpenAI Go library provides convenient access to [the OpenAI REST
API](https://platform.openai.com/docs) from applications written in Go. The full API of this library can be found in [api.md](api.md).

> [!WARNING]
> The latest version of this package uses a new design with significant breaking changes.
> Please refer to the [migration guide](./MIGRATION.md) for more information on how to update your code.

## Installation

<!-- x-release-please-start-version -->
Expand All @@ -22,7 +26,7 @@ Or to pin the version:
<!-- x-release-please-start-version -->

```sh
go get -u 'github.com/openai/[email protected]alpha.67'
go get -u 'github.com/openai/[email protected]beta.1'
```

<!-- x-release-please-end -->
Expand Down Expand Up @@ -342,8 +346,8 @@ Otherwise, the `param.IsOmitted(any)` function can confirm the presence of any `
Unions are represented as a struct with fields prefixed by "Of" for each of it's variants,
only one field can be non-zero. The non-zero field will be serialized.

Properties can be accessed via getters on the union struct. These getters return a mutable
pointer to the underlying data, if present.
Sub-properties of the union can be accessed via methods on the union struct.
These methods return a mutable pointer to the underlying data, if present.

```go
// Only one field can be non-zero, use param.IsOmitted() to check if a field is set
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package internal

const PackageVersion = "0.1.0-alpha.67" // x-release-please-version
const PackageVersion = "0.1.0-beta.1" // x-release-please-version