openai-php / openai-php/client

[Bug]: CreateStreamedResponse fails with custom models that return sources without choices

Open
#545 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Non-OpenAI Model
Dominant language
PHP
Stars
5.8k
Forks
679
Avg merge
19m
Merged PRs (30d)
2

Description

Description

Bug: CreateStreamedResponse fails with custom models that return sources without choices

Issue Description

When using custom models through Open WebUI that return sources in the first response without a 'choices' array, the CreateStreamedResponse::from() method fails because it can't find the expected 'choices' key in the response attributes.

Steps To Reproduce

Current Behavior

The first response from a custom model using sources returns something like:

{
    "sources": [
        {
            "source": {
                "id": "b3d24ed7-a1f1-4054-bb37-55c9cf96ad70",
                "name": "Example Document",
                "type": "collection"
            },
            "document": ["Content example..."],
            "metadata": [
                {
                    "name": "Example File.txt",
                    "source": "Example File.txt"
                }
            ],
            "distances": [0.8338083668559383]
        }
    ]
}

This causes the following code in CreateStreamedResponse::from() to fail:

public static function from(array $attributes): self
{
    $choices = array_map(fn (array $result): CreateStreamedResponseChoice => CreateStreamedResponseChoice::from(
        $result
    ), $attributes['choices']);

    return new self(
        $attributes['id'],
        $attributes['object'],
        $attributes['created'],
        $attributes['model'],
        $choices,
        isset($attributes['usage']) ? CreateResponseUsage::from($attributes['usage']) : null,
    );
}

The error occurs because $attributes['choices'] doesn't exist in the response, causing the stream to fail instead of gracefully handling or skipping this response chunk.

Expected Behavior

The client should be able to handle response chunks that don't contain the 'choices' field by either:

  1. Skipping chunks that don't have a 'choices' field, or
  2. Adding support for the 'sources' field in the response format

This would allow the client to work with custom models that return sources information, particularly in the first chunk of a streamed response.

Environment

  • Package version: v0.10.3
  • PHP version: 8.3
  • Using with: Open WebUI custom model with knowledges
OpenAI PHP Client Version

v0.10.3

PHP Version

8.3.16

Notes

This issue specifically affects custom models in Open WebUI that return sources information. The standard OpenAI API doesn't have this issue, but as more people use the client with alternative backends, handling non-standard response formats becomes important for compatibility.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with CreateStreamedResponse::from() and trace how streamed response chunks are parsed. Reproduce the Open WebUI response containing sources without choices, then verify that such a chunk is handled without failing while standard responses remain supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.