Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(php-sdk): avoid buffering streaming responses (#44271)

Winoreneff2623b25c8
diff --git a/sdks/php-client/README.md b/sdks/php-client/README.md
index d318e89a4cf..6c5c7ca3a32 100644
--- a/sdks/php-client/README.md
+++ b/sdks/php-client/README.md
@@ -94,6 +94,36 @@ $response = $difyClient->message_feedback($message_id, $rating, "user_id");
Replace 'your-api-key-here' with your actual Dify API key.
+## Streaming responses
+
+Pass `streaming` as the response mode to receive a Guzzle response without waiting for
+all workflow events to arrive. Read its body incrementally; individual reads may split
+an SSE event across chunks.
+
+```php
+$workflowClient = new WorkflowClient($apiKey);
+$response = $workflowClient->run(array("query" => "hello"), "streaming", "user_id");
+$body = $response->getBody();
+while (!$body->eof()) {
+ echo $body->read(1024);
+}
+```
+
+Use `blocking` for the complete JSON response.
+
+For text-to-audio, pass `true` to consume audio without buffering the complete response:
+
+```php
+$response = $difyClient->text_to_audio("Hello", "user_id", true);
+$body = $response->getBody();
+while (!$body->eof()) {
+ echo $body->read(1024);
+}
+```
+
+This controls client-side buffering. Whether the server produces audio incrementally
+depends on the TTS provider output.
+
## Empty app inputs
Apps without custom input variables can be called with an empty PHP array:
@@ -117,6 +147,7 @@ After `composer install`, run these commands from this directory:
```sh
php tests/base_url_test.php
php tests/input_object_test.php
+php tests/streaming_test.php
```
The regressions use Guzzle's mock transport and do not contact an API or model provider.
diff --git a/sdks/php-client/dify-client.php b/sdks/php-client/dify-client.php
index cfbdfbb9ec7..8b2c4d07d0c 100644
--- a/sdks/php-client/dify-client.php
+++ b/sdks/php-client/dify-client.php
@@ -81,7 +81,7 @@ class DifyClient {
'streaming' => $streaming
];
- return $this->send_request('POST', 'text-to-audio', $data);
+ return $this->send_request('POST', 'text-to-audio', $data, null, $streaming);
}
public function get_meta($user) {
@@ -194,7 +194,7 @@ class WorkflowClient extends DifyClient{
'response_mode' => $response_mode,
'user' => $user,
];
- return $this->send_request('POST', 'workflows/run', $data);
+ return $this->send_request('POST', 'workflows/run', $data, null, $response_mode === 'streaming');
}
public function stop($task_id, $user) {
diff --git a/sdks/php-client/tests/streaming_test.php b/sdks/php-client/tests/streaming_test.php
new file mode 100644
index 00000000000..be729137eea
--- /dev/null
+++ b/sdks/php-client/tests/streaming_test.php
@@ -0,0 +1,79 @@
+<?php
+
+require dirname(__DIR__) . '/vendor/autoload.php';
+
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\Middleware;
+use GuzzleHttp\Psr7\Response;
+
+$client = new class('test-key') extends WorkflowClient {
+ public function recordRequests(array &$history): void {
+ // Keep the SDK's real Guzzle defaults and middleware; replace only transport.
+ $stack = $this->client->getConfig('handler');
+ $stack->setHandler(new MockHandler([
+ new Response(200, ['Content-Type' => 'text/event-stream'], "data: {}\n\n"),
+ new Response(200, ['Content-Type' => 'application/json'], '{}'),
+ ]));
+ $stack->push(Middleware::history($history));
+ }
+};
+$history = [];
+$client->recordRequests($history);
+
+foreach (['streaming', 'blocking'] as $index => $mode) {
+ $response = $client->run(['text' => 'hello'], $mode, 'test-user');
+ $transaction = $history[$index];
+ $request = $transaction['request'];
+ if ($transaction['options']['stream'] !== ($mode === 'streaming')) {
+ throw new RuntimeException('Wrong Guzzle stream option for response_mode=' . $mode);
+ }
+ if ($request->getMethod() !== 'POST' || $request->getUri()->getPath() !== '/v1/workflows/run') {
+ throw new RuntimeException('Workflow endpoint changed');
+ }
+ if (json_decode((string) $request->getBody(), true) !== [
+ 'inputs' => ['text' => 'hello'],
+ 'response_mode' => $mode,
+ 'user' => 'test-user',
+ ]) {
+ throw new RuntimeException('Workflow JSON payload changed');
+ }
+ if ($request->getHeaderLine('Authorization') !== 'Bearer test-key' || $response->getStatusCode() !== 200) {
+ throw new RuntimeException('Authentication or response changed');
+ }
+}
+
+echo "Workflow streaming and blocking request checks passed.\n";
+
+
+foreach ([true, false] as $streaming) {
+ $client = new class('test-key') extends DifyClient {
+ public function recordRequests(array &$history): void {
+ $stack = $this->client->getConfig('handler');
+ $stack->setHandler(new MockHandler([new Response(200, ['Content-Type' => 'audio/wav'], 'audio')]));
+ $stack->push(Middleware::history($history));
+ }
+ };
+ $history = [];
+ $client->recordRequests($history);
+ $response = $client->text_to_audio('hello', 'test-user', $streaming);
+ $transaction = $history[0];
+ $request = $transaction['request'];
+ if ($transaction['options']['stream'] !== $streaming) {
+ throw new RuntimeException('Wrong Guzzle stream option for text_to_audio');
+ }
+ if ($request->getMethod() !== 'POST' || $request->getUri()->getPath() !== '/v1/text-to-audio') {
+ throw new RuntimeException('Text-to-audio endpoint changed');
+ }
+ if (json_decode((string) $request->getBody(), true) !== [
+ 'text' => 'hello',
+ 'user' => 'test-user',
+ 'streaming' => $streaming,
+ ]) {
+ throw new RuntimeException('Text-to-audio JSON payload changed');
+ }
+ if ($request->getHeaderLine('Authorization') !== 'Bearer test-key' || $response->getStatusCode() !== 200) {
+ throw new RuntimeException('Authentication or response changed');
+ }
+}
+
+echo "Text-to-audio streaming and buffered request checks passed.\n";