Skip to content

Fix/optional usage metadata - #10

Merged
HenriqueAssumpcao merged 2 commits into
inter-science:mainfrom
HenriqueAssumpcao:fix/optional-usage-metadata
Nov 10, 2026
Merged

HenriqueAssumpcao merged 2 commits into
inter-science:mainfrom
HenriqueAssumpcao:fix/optional-usage-metadata

Conversation

@HenriqueAssumpcao

Copy link
Copy Markdown
Collaborator

Summary

Fixes #9. OpenAILM.generate read ret.usage.prompt_tokens inside its retry block. When an OpenAI-compatible provider leaves out the optional usage field, a completion that already succeeded raised AttributeError. It was then resent retries + 1 times (billed each time) and finally thrown away as a ConnectionError. OpenAIEmbedding.embed_batch had the same problem with total_tokens.

Changes

  • Missing usage: only the request itself is retried. Once it succeeds, missing or partial usage gives None token counts, and the ensemble logs a warning.
  • Empty responses: a response with no choices, or with the wrong number of embedding vectors, raises a clear EmptyResponseError and is still retried.
  • Errors that can't succeed on retry: 400, 401, 403, 404 and 422 fail right away. 429 and 5xx are still retried with backoff.
  • Type hints: updated to Optional[int] token counts, which also fixes the ensemble's return annotation that had the wrong element order.
  • Flaky test: fixed a race in test_queue_handler_truncated, where get_nowait() ran before the queue's background thread had delivered the message. It failed in about 80% of local runs.

Tests

  • 24 new offline tests in tests/test_lm.py run the real AsyncOpenAI client against httpx.MockTransport responses and count HTTP attempts.
  • 13 of them fail on the old code.
  • The full suite passes, 237 of 237, on repeated runs.

HenriqueAssumpcao and others added 2 commits November 10, 2026 20:05
OpenAILM.generate read `ret.usage.prompt_tokens` inside its retry block.
With OpenAI-compatible providers that omit the optional `usage` field,
a successful (billed) completion raised AttributeError, was retried
retries+1 times, and was finally discarded as a ConnectionError.
OpenAIEmbedding.embed_batch had the same problem with `total_tokens`.

- Only the request itself is retried; once it succeeds, missing or
  partial usage is reported as None token counts instead of an error.
  The ensemble logs a warning through the island logger.
- Responses with no choices (or the wrong number of embedding vectors)
  raise a clear EmptyResponseError and are still retried.
- Non-retryable client errors (400/401/403/404/422) fail immediately
  instead of going through the backoff loop; 429/5xx are still retried.
- Update BaseLM/BaseEnsemble/BaseEmbedding hints to Optional[int]
  token counts, and fix the ensemble's mis-ordered return annotation.
- Add offline tests using the real AsyncOpenAI client over
  httpx.MockTransport fixtures, counting HTTP attempts.

Refs inter-science#9

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mp.Queue.put() hands items to a background feeder thread, so calling
get_nowait() immediately afterwards intermittently raised queue.Empty
(failing ~80% of local runs). Wait for the item with a timeout instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@HenriqueAssumpcao
HenriqueAssumpcao merged commit 27d3588 into inter-science:main Nov 10, 2026
2 checks passed
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.

1 participant