Skip to content

dialogflow: fix detect-intent-stream losing the query result - #2336

Open
rootkiller6788 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
rootkiller6788:fix-dialogflow-stream-query-result
Open

rootkiller6788 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
rootkiller6788:fix-dialogflow-stream-query-result

Conversation

@rootkiller6788

Copy link
Copy Markdown

The streaming sample threw away the answer whenever output audio was on.

detect_intent_stream() only looked at the last response from the stream. When you ask Dialogflow for synthesised audio the server sends the query result first and then keeps pushing audio chunks, so the last response has no query result and the sample died with Call to a member function getQueryText() on null - the same thing reported in #911 and upstream in googleapis/google-cloud-php#2026.

It also read $response after the foreach, which is undefined when the stream comes back empty.

So instead: pick up the query result as the responses come in and guard on that. I ran the sample function against a stubbed client to check each case - plain stream gives byte-identical output to before, output audio now prints the intent/fulfilment text, and an empty stream no longer warns about $response.

The response stream doesn't always end with the query result. With output
audio enabled the server sends the result first and then keeps streaming
audio chunks, so looking at the last response alone died with "Call to a
member function getQueryText() on null" and threw away the whole answer
the caller asked for.

Collect the result as the responses come in instead, and stop reading
$response after the loop since it's undefined when the stream is empty.
@product-auto-label product-auto-label Bot added api: dialogflow Issues related to the Dialogflow API. samples Issues that are directly related to samples. labels Oct 12, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates detect_intent_stream.php to correctly capture the queryResult from stream responses, addressing cases where the server continues streaming audio after sending the result. The review feedback highlights a potential issue with using the Elvis operator ?: on $response->getQueryResult(), as empty Protobuf message objects are truthy and could overwrite a valid $queryResult. The reviewer recommends using hasQueryResult() to safely verify the field's presence.

$transcript = $recognitionResult->getTranscript();
printf('Intermediate transcript: %s' . PHP_EOL, $transcript);
}
$queryResult = $response->getQueryResult() ?: $queryResult;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Using the Elvis operator ?: on $response->getQueryResult() can be risky. In PHP Protobuf, if the server sends an empty query_result field or if the getter returns an empty message object instead of null, the object will be truthy. This would overwrite the previously stored valid $queryResult with an empty one.

Using hasQueryResult() is the standard and safe way to check if a message field is present in the response.

        if ($response->hasQueryResult()) {
            $queryResult = $response->getQueryResult();
        }

@rootkiller6788
rootkiller6788 marked this pull request as ready for review October 12, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: dialogflow Issues related to the Dialogflow API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant