From e0cd86ce01004b415bb9208dbf7d3a97b0532a1f Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Tue, 27 Oct 2026 01:12:36 +0530 Subject: [PATCH 1/2] fix(files): prevent infinite loop on failed video processing and guard against nil response in Go - In Go, Python, and JavaScript files samples, poll loops checking for ACTIVE state previously hung indefinitely if a video file entered the FAILED state. Add explicit checks for FAILED state to error out immediately. - In go/test_utils.go, guard printResponse against nil *genai.GenerateContentResponse to prevent nil pointer dereference panics. - Add unit tests for printResponse nil and empty handling in go/test_utils_test.go. Signed-off-by: reginaldalfret --- go/files.go | 5 ++++- go/test_utils.go | 3 +++ go/test_utils_test.go | 32 ++++++++++++++++++++++++++++++++ javascript/files.js | 5 +++++ python/files.py | 2 ++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 go/test_utils_test.go diff --git a/go/files.go b/go/files.go index cff2004..c7c4e6c 100644 --- a/go/files.go +++ b/go/files.go @@ -160,7 +160,10 @@ func FilesCreateVideo() (*genai.GenerateContentResponse, error) { fmt.Printf("myfile=%+v\n", myfile) // Poll until the video file is completely processed (state becomes ACTIVE). - for myfile.State == genai.FileStateUnspecified || myfile.State != genai.FileStateActive { + for myfile.State != genai.FileStateActive { + if myfile.State == genai.FileStateFailed { + log.Fatalf("video file processing failed: %v", myfile.State) + } fmt.Println("Processing video...") fmt.Println("File state:", myfile.State) time.Sleep(5 * time.Second) diff --git a/go/test_utils.go b/go/test_utils.go index fb1a0e1..87b3cbb 100644 --- a/go/test_utils.go +++ b/go/test_utils.go @@ -25,6 +25,9 @@ func getMedia() string { // Helping for printing the response. func printResponse(resp *genai.GenerateContentResponse) { + if resp == nil { + return + } for _, cand := range resp.Candidates { if cand.Content != nil { for _, part := range cand.Content.Parts { diff --git a/go/test_utils_test.go b/go/test_utils_test.go new file mode 100644 index 0000000..7697fba --- /dev/null +++ b/go/test_utils_test.go @@ -0,0 +1,32 @@ +package examples + +import ( + "testing" + + "google.golang.org/genai" +) + +func TestPrintResponseNil(t *testing.T) { + // Must not panic on nil response + printResponse(nil) +} + +func TestPrintResponseEmpty(t *testing.T) { + resp := &genai.GenerateContentResponse{} + printResponse(resp) +} + +func TestPrintResponseWithCandidates(t *testing.T) { + resp := &genai.GenerateContentResponse{ + Candidates: []*genai.Candidate{ + { + Content: &genai.Content{ + Parts: []*genai.Part{ + {Text: "test output"}, + }, + }, + }, + }, + } + printResponse(resp) +} diff --git a/javascript/files.js b/javascript/files.js index 31cd378..7094149 100644 --- a/javascript/files.js +++ b/javascript/files.js @@ -110,6 +110,11 @@ export async function filesCreateVideo() { // Poll until the video file is completely processed (state becomes ACTIVE). while (!myfile.state || myfile.state.toString() !== 'ACTIVE') { + if (myfile.state && myfile.state.toString() === 'FAILED') { + throw new Error( + `Video file processing failed: ${myfile.error?.message || 'state is FAILED'}`, + ); + } console.log('Processing video...'); console.log('File state: ', myfile.state); await sleep(5000); diff --git a/python/files.py b/python/files.py index c462104..1a12976 100644 --- a/python/files.py +++ b/python/files.py @@ -80,6 +80,8 @@ def test_files_create_video(self): # Poll until the video file is completely processed (state becomes ACTIVE). while not myfile.state or myfile.state.name != "ACTIVE": + if myfile.state and myfile.state.name == "FAILED": + raise ValueError(f"Video file processing failed: {getattr(myfile, 'error', 'state is FAILED')}") print("Processing video...") print("File state:", myfile.state) time.sleep(5) From fa4895c74cf1f2228dac743cd819f8c8d5260a45 Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Tue, 27 Oct 2026 11:12:33 +0530 Subject: [PATCH 2/2] fix(files): return error in Go and improve error fallback in Python on video failure Signed-off-by: reginaldalfret --- go/files.go | 2 +- python/files.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go/files.go b/go/files.go index c7c4e6c..5f4f576 100644 --- a/go/files.go +++ b/go/files.go @@ -162,7 +162,7 @@ func FilesCreateVideo() (*genai.GenerateContentResponse, error) { // Poll until the video file is completely processed (state becomes ACTIVE). for myfile.State != genai.FileStateActive { if myfile.State == genai.FileStateFailed { - log.Fatalf("video file processing failed: %v", myfile.State) + return nil, fmt.Errorf("video file processing failed: %v", myfile.State) } fmt.Println("Processing video...") fmt.Println("File state:", myfile.State) diff --git a/python/files.py b/python/files.py index 1a12976..f9bf532 100644 --- a/python/files.py +++ b/python/files.py @@ -81,7 +81,7 @@ def test_files_create_video(self): # Poll until the video file is completely processed (state becomes ACTIVE). while not myfile.state or myfile.state.name != "ACTIVE": if myfile.state and myfile.state.name == "FAILED": - raise ValueError(f"Video file processing failed: {getattr(myfile, 'error', 'state is FAILED')}") + raise ValueError(f"Video file processing failed: {getattr(myfile, 'error', None) or 'state is FAILED'}") print("Processing video...") print("File state:", myfile.state) time.sleep(5)