Skip to content

fix: preserve usage-only OpenAI streaming chunks - #2164

Open
cenab wants to merge 1 commit into
FoundationAgents:mainfrom
cenab:fix/openai-stream-usage-20260928
Open

cenab wants to merge 1 commit into
FoundationAgents:mainfrom
cenab:fix/openai-stream-usage-20260928

Conversation

@cenab

@cenab cenab commented Sep 28, 2026 •

Copy link
Copy Markdown

Features

Preserve server-reported token usage from streaming chunks with an empty
choices array. Previously the parser skipped these chunks and fell back to a
local estimate, which can produce zero counts for an unknown model. The change
handles both SDK usage objects and dictionaries before skipping the empty
choices.

Closes #2163.

Feature Docs

The reproduction and expected behavior are in #2163.

Influence

This fixes the shared OpenAILLM stream parser. Text assembly, existing usage
on finish chunks or choices, and the missing-usage fallback retain their
behavior. The new regression module is collected by the current pytest config;
the older test_openai.py filename is excluded there.

Result

  • Six focused regression cases pass with the normal root conftest. On unchanged
    base 11cdf466, exactly the two usage-only cases fail and four controls pass.
  • Twenty controlled native HTTP cases pass across JSON, SSE, aask and
    aask_code, including two custom aliases and 401 responses. Returned server
    usage is now 4 prompt / 2 completion tokens for JSON and SSE. These tests use
    a local fixture with synthetic credentials; no live model claim is made.
  • Pinned Black 23.3.0, isort 5.11.5 and Ruff 0.0.284 checks pass across all
    tracked Python files plus the new regression module. git diff --check passes.
  • Existing focused OpenAI tests: six pass; test_count_tokens expects 47 and
    gets 33 on both unchanged base and this patch in the same environment.
  • Full-suite collection with upstream options encounters missing full-suite
    dependencies (including llama_index, faiss and aioboto3). The full local
    suite has not passed. Validation used Python 3.11.13 and OpenAI 1.64.0;
    upstream CI's Python 3.9 run remains pending.

Focused command, with native synthetic project config and live calls disabled:

ALLOW_OPENAI_API_CALL=0 pytest -o addopts= tests/metagpt/provider/test_openai_stream_usage.py -q

Other

Repository issue
#2161 reports fork checkout failures in the current pull_request_target
workflows. This candidate’s fulltest run
also failed at actions/checkout@v4, before Python setup or tests; the other
workflow is waiting for approval. This PR contains the parser change and
regression tests only and preserves those CI protections.

This branch had an error being deployed

1 failed deployment
unittest — b9ac39de Deployed Sep 28, 2026 by cenab via build (3.9) #1739
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAILLM ignores usage-only streaming chunks with empty choices

1 participant