Skip to content

[WIP] Fix streamGenerateContent returning null in HttpTransporter#151

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/fix-stream-generate-content-issue
Closed

[WIP] Fix streamGenerateContent returning null in HttpTransporter#151
Copilot wants to merge 1 commit intomainfrom
copilot/fix-stream-generate-content-issue

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 21, 2025

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>streamGenerateContent returns null instead of Psr\Http\Message\ResponseInterface (Fatal TypeError in HttpTransporter::requestStream)</issue_title>
<issue_description>## Describe the bug

Calling streamGenerateContent() causes a fatal TypeError inside the library’s internal HttpTransporter::requestStream() method.

Although the user-defined stream handler is invoked correctly and receives streamed chunks, the requestStream() method itself fails to return the required Psr\Http\Message\ResponseInterface object. Instead, it returns null, violating the function’s declared return type and terminating execution with a fatal error.

This occurs inside the closure used by HttpTransporter::sendRequest(), specifically in:

vendor/google-gemini-php/client/src/Transporters/HttpTransporter.php:68

(or near the end of the requestStream() method depending on the version).


To Reproduce

  1. Install the library via Composer (version tested: 2.7.0).
  2. Use the following code:
// The stream handler is correctly defined for side-effect output
$streamHandler = function (string $chunk): void {
    echo $chunk;
};

$client = \Gemini::factory()
    ->withApiKey($apiKey)
    ->withStreamHandler($streamHandler)
    ->make();

$result = $client
    ->generativeModel('gemini-2.0-flash')
    ->streamGenerateContent('Generate a short story.');

  1. Run the script.

Expected behavior

  • The streaming process should complete without fatal errors.
  • requestStream() should return a valid Psr\Http\Message\ResponseInterface instance internally.
  • Even if no final response body is produced, the function must not return null due to its strict return type.

Actual behavior

The PHP runtime terminates with:

PHP Fatal error: Uncaught TypeError: 
Gemini\Transporters\HttpTransporter::{closure:Gemini\Transporters\HttpTransporter::requestStream():68}(): 
Return value must be of type Psr\Http\Message\ResponseInterface, null returned 
in vendor/google-gemini-php/client/src/Transporters/HttpTransporter.php:68

Full stack trace confirms this originates entirely from internal library logic and not from the user-provided stream handler.


Workaround

Switch to the synchronous request method:

$result = $client
    ->generativeModel('gemini-2.0-flash')
    ->generateContent('Generate a short story.');


Environment

  • PHP Version: 8.4.14
  • Client Library Version: 2.7.0
  • Operating System: Windows 11

Additional context

This behavior indicates a missing return statement or an unhandled branch in the streaming transport path.
The closure created inside requestStream() declares a Psr\Http\Message\ResponseInterface return type but returns null, which is guaranteed to fail under strict typing.

I can provide additional logs or a reproducible minimal case if needed.
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

streamGenerateContent returns null instead of Psr\Http\Message\ResponseInterface (Fatal TypeError in HttpTransporter::requestStream)

2 participants