Streamed chat completions break when the backend answers with `Content-Encoding: br`

Aperta Adatta ai principianti
#471 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
92/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
api

Direzione di ricerca

Inizia da OpenAiAPIService::request() e dal suo ramo per le richieste in streaming, in cui l'intestazione Accept-Encoding viene impostata per le richieste compatibili con OpenAI. Il issue indica la correzione di una riga e spiega perché Content-Encoding: br fa sì che json_decode() restituisca null; verifica che le risposte in streaming più lunghe della soglia di compressione vengano analizzate correttamente dopo la modifica.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Opening this here as a cross-reference to nextcloud/server#67009, since the symptom shows up in this app.

What happens

With an OpenAI-compatible backend that compresses its responses with brotli (any
Express service using the standard compression middleware does this for bodies
over 1 KiB), every Assistant chat whose answer exceeds that threshold fails with:

integration_openai  Could not JSON parse the response   {"body":null}
OCP\TaskProcessing\Exception\ProcessingException:
  OpenAI/LocalAI request failed: Could not JSON parse the response

Short answers work, longer ones never do. The body: null in the log is a
red herring: the body is binary, so the logger cannot serialise it.

Why

OpenAiAPIService opts into Guzzle's StreamHandler:

// chooseHandler returns a wrapper that chooses the StreamHandler if the request 'stream' option is set
$this->client = $clientService->newClient(Utils::chooseHandler());

and assistant always sets setPreferStreaming(true), so chat requests take the
streaming path. Nextcloud's HTTP client advertises Accept-Encoding: br, gzip
(based on libcurl's feature flags, server 34+), but StreamHandler can only
inflate gzip/deflate. When the backend honours br, the raw brotli bytes reach
json_decode() with Content-Type: application/json still set, and parsing
returns null.

The same request without 'stream' => true works, because that one goes through
the curl handler, which decodes brotli transparently.

Suggested fix here

Regardless of what server decides, this app could set the header itself for
streamed requests, in request() inside the if ($stream) branch:

$options['headers']['Accept-Encoding'] = 'identity';

(Client::buildRequestOptions() only adds Accept-Encoding when it is not
already present, so a pre-set value is honoured.) That is the one-line local
patch we are running.

Environment
  • Nextcloud 35.0.3, integration_openai 5.0.0, assistant 4.1.0
  • PHP 8.5.4, libcurl 8.18.0 with brotli 1.2.0
Lingua principale
PHP
Stelle
78
Fork
42
Merge medio
2g 21h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nextcloud/integration_openai

Tutte le issue di nextcloud/integration_openai

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.