Skip to content
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

feat: type BatchStatus for all possible states #907

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 42 additions & 25 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ const (
BatchEndpointEmbeddings BatchEndpoint = "/v1/embeddings"
)

type BatchStatus string

const (
BatchStatusValidating BatchStatus = "validating"
BatchStatusFailed BatchStatus = "failed"
BatchStatusInProgress BatchStatus = "in_progress"
BatchStatusFinalizing BatchStatus = "finalizing"
BatchStatusCompleted BatchStatus = "completed"
BatchStatusExpired BatchStatus = "expired"
BatchStatusCancelling BatchStatus = "cancelling"
BatchStatusCancelled BatchStatus = "cancelled"
)

type BatchLineItem interface {
MarshalBatchLineItem() []byte
}
Expand Down Expand Up @@ -60,36 +73,40 @@ func (r BatchEmbeddingRequest) MarshalBatchLineItem() []byte {
}

type Batch struct {
ID string `json:"id"`
Object string `json:"object"`
Endpoint BatchEndpoint `json:"endpoint"`
Errors *struct {
Object string `json:"object,omitempty"`
Data []struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Param *string `json:"param,omitempty"`
Line *int `json:"line,omitempty"`
} `json:"data"`
} `json:"errors"`
ID string `json:"id"`
Object string `json:"object"`
InputFileID string `json:"input_file_id"`
CompletionWindow string `json:"completion_window"`
Status string `json:"status"`
OutputFileID *string `json:"output_file_id"`
ErrorFileID *string `json:"error_file_id"`
CreatedAt int `json:"created_at"`
InProgressAt *int `json:"in_progress_at"`
ExpiresAt *int `json:"expires_at"`
FinalizingAt *int `json:"finalizing_at"`
CompletedAt *int `json:"completed_at"`
FailedAt *int `json:"failed_at"`
ExpiredAt *int `json:"expired_at"`
CancellingAt *int `json:"cancelling_at"`
CancelledAt *int `json:"cancelled_at"`
RequestCounts BatchRequestCounts `json:"request_counts"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tucnak, Could you please re-format this PR? Such huge diffs are very hard to review — I need to write a script to verify that no fields are getting removed.

With a single field's type changed, the diff shouldn't be much larger than an example below:

diff --git a/batch.go b/batch.go
index 3c1a9d0..07d394d 100644
--- a/batch.go
+++ b/batch.go
@@ -59,19 +59,25 @@ func (r BatchEmbeddingRequest) MarshalBatchLineItem() []byte {
 	return marshal
 }
 
+type BatchStatus string
+
+const (
+	BatchStatusValidating BatchStatus = "validating"
+	BatchStatusFailed     BatchStatus = "failed"
+	BatchStatusInProgress BatchStatus = "in_progress"
+	BatchStatusFinalizing BatchStatus = "finalizing"
+	BatchStatusCompleted  BatchStatus = "completed"
+	BatchStatusExpired    BatchStatus = "expired"
+	BatchStatusCancelling BatchStatus = "cancelling"
+	BatchStatusCancelled  BatchStatus = "cancelled"
+)
+
+
 type Batch struct {
 	ID       string        `json:"id"`
 	Object   string        `json:"object"`
 	Endpoint BatchEndpoint `json:"endpoint"`
-	Errors   *struct {
-		Object string `json:"object,omitempty"`
-		Data   []struct {
-			Code    string  `json:"code,omitempty"`
-			Message string  `json:"message,omitempty"`
-			Param   *string `json:"param,omitempty"`
-			Line    *int    `json:"line,omitempty"`
-		} `json:"data"`
-	} `json:"errors"`
+	Errors           *BatchErrors       `json:"errors,omitempty"`
 	InputFileID      string             `json:"input_file_id"`
 	CompletionWindow string             `json:"completion_window"`
 	Status           string             `json:"status"`
@@ -90,6 +96,20 @@ type Batch struct {
 	Metadata         map[string]any     `json:"metadata"`
 }
 
+
+type BatchErrors struct {
+	Object string       `json:"object,omitempty"`
+	Data   []BatchError `json:"data"`
+}
+
+type BatchError struct {
+	Code    string `json:"code,omitempty"`
+	Message string `json:"message,omitempty"`
+	Param   string `json:"param,omitempty"`
+	Line    int    `json:"line,omitempty"`
+}
+
+
 type BatchRequestCounts struct {
 	Total     int `json:"total"`
 	Completed int `json:"completed"`

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm sorry I had accidentally cherry-picked a different commit into this branch.

I can rebase the last commit away, which is what brings most changes, but what do you think in general, is it perhaps worth merging either way? The Errors field is currently inline, & it was a serious issue for me when implementing a batching proxy (as I need to manually aggregate the errors.)

I also felt the optional timestamps are better served with omitempty (int64 zero is non-ambiguous) and it's really tedious to handle int64 pointers in general (as opposed to timestamps.) FWIW, I also have this type which is based on time.Time implementing json.Marshaler and json.Unmarshaler on integers. Don't know how willing you are to introduce breaking changes for future major versions, but I think that one is marginally worth it: handling time.Time conversions is quite tedious, and having a type is beneficial to that end, as both functional time.Time and JSON-transparent (you get int64 in and out.)

Endpoint BatchEndpoint `json:"endpoint"`
Status BatchStatus `json:"status"`
Errors *BatchErrors `json:"errors,omitempty"`
OutputFileID string `json:"output_file_id,omitempty"`
ErrorFileID string `json:"error_file_id,omitempty"`
CreatedAt int64 `json:"created_at"`
InProgressAt int64 `json:"in_progress_at,omitempty"`
ExpiresAt int64 `json:"expires_at,omitempty"`
FinalizingAt int64 `json:"finalizing_at,omitempty"`
CompletedAt int64 `json:"completed_at,omitempty"`
FailedAt int64 `json:"failed_at,omitempty"`
ExpiredAt int64 `json:"expired_at,omitempty"`
CancellingAt int64 `json:"cancelling_at,omitempty"`
CancelledAt int64 `json:"cancelled_at,omitempty"`
RequestCounts BatchRequestCounts `json:"request_counts,omitempty"`
Metadata map[string]any `json:"metadata"`
}

type BatchErrors struct {
Object string `json:"object,omitempty"`
Data []BatchError `json:"data"`
}

type BatchError struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Param string `json:"param,omitempty"`
Line int `json:"line,omitempty"`
}

type BatchRequestCounts struct {
Total int `json:"total"`
Completed int `json:"completed"`
Expand Down
Loading