Repository navigation
dialogflow: fix detect-intent-stream losing the query result - #2336
rootkiller6788 wants to merge 1 commit into
Conversation
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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();
}
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.