xtekky/gpt4free · error · RuntimeError

Response: {line}

Error message

Response: {line}

What it means

Raised while iterating HuggingChat's streaming response when a JSON line decodes successfully but has no "type" field. The SSE/stream protocol from huggingface.co conversations is expected to carry typed events (stream, finalAnswer, file, reasoning); an untyped line means the protocol changed or an unexpected event shape arrived, and the provider fails fast with the raw line included.

Source

Thrown at g4f/Provider/needs_auth/hf/HuggingChat.py:178

        response = session.post(
            f"{cls.url}/conversation/{conversationId}",
            headers=headers,
            multipart=data,
            stream=True,
        )
        raise_for_status(response)

        sources = None
        for line in response.iter_lines():
            if not line:
                continue
            try:
                line = json.loads(line)
            except json.JSONDecodeError as e:
                debug.error(f"Failed to decode JSON: {line}, error: {e}")
                continue
            if "type" not in line:
                raise RuntimeError(f"Response: {line}")
            elif line["type"] == "stream":
                yield line["token"].replace("\u0000", "")
            elif line["type"] == "finalAnswer":
                if sources is not None:
                    yield sources
                yield FinishReason("stop")
                break
            elif line["type"] == "file":
                url = f"{cls.url}/conversation/{conversationId}/output/{line['sha']}"
                yield ImageResponse(
                    url,
                    format_media_prompt(messages, prompt),
                    options={"cookies": auth_result.cookies},
                )
            elif line["type"] == "webSearch" and "sources" in line:
                sources = Sources(line["sources"])
            elif line["type"] == "title":
                yield TitleGeneration(line["title"])

View on GitHub (pinned to 973504e177)

Solutions

  1. Update g4f — protocol drift against HuggingChat's stream is handled in newer releases
  2. Inspect the logged line to see what the untyped event contains
  3. Check HuggingFace status/changelog for conversation API changes
  4. Fall back to a different chat provider while the fix lands
Defensive patterns

Strategy: fallback

Try / catch

try:
    async for chunk in HuggingChat.create_async_generator(model, messages, auth_result=auth):
        ...
except RuntimeError as e:
    if str(e).startswith("Response:"):
        # untyped stream event: protocol drift — switch provider
        async for chunk in fallback_provider.create_async_generator(model, messages):
            ...
    else:
        raise

Prevention

When it happens

Trigger: Streaming a HuggingChat conversation where response.iter_lines() yields a valid JSON object lacking the "type" key (e.g. HuggingFace adds a new event kind or returns an error object inline).

Common situations: HuggingFace ships a streaming protocol change (new event types, error frames); g4f version lagging behind the API; account-level notices injected into the stream.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/ef326b285c5c0016. Report an issue: GitHub.