Skip to content

fix: allow null stop_reason in Messages stream message_start - #179

Merged
edenreich merged 1 commit into
mainfrom
fix/issue-171-20261117-1939
Nov 17, 2026
Merged

edenreich merged 1 commit into
mainfrom
fix/issue-171-20261117-1939

Conversation

@inference-gateway-maintainer

Copy link
Copy Markdown
Contributor

Summary

create_message_stream raised InferenceGatewayValidationError on the first Anthropic stream event because message_start carries "stop_reason": null, and MessagesResponse.stop_reason was a required non-nullable literal.

Changes

  • openapi.yaml: mark MessagesResponse.stop_reason as nullable: true (still required).
  • inference_gateway/models.py (generated code): stop_reason is now Literal[...] | None. This matches what datamodel-codegen --strict-nullable emits for a required nullable field. The model edit was made by hand because the sandbox could not run datamodel-codegen. A maintainer may want to rerun the codegen command from task generate without the download step to confirm there is no diff.
  • tests/test_client.py: test_create_message_stream now uses the real message_start shape (stop_reason: null, stop_sequence: null) and asserts stop_reason is None.

Generated code / compatibility

  • This edits generated code. The upstream fix belongs in inference-gateway/schemas. Until that lands, running task generate will download the old spec and revert this fix. Follow-up issue: [BUG] MessagesResponse.stop_reason must be nullable for streaming message_start schemas#327.
  • API compatibility: MessagesResponse.stop_reason is now typed ... | None. Code that type-checks against the old non-optional type may need a None check. Runtime behavior for non-streaming responses is unchanged.
  • Downstream: other SDKs generated from the same schema (typescript, rust, go) probably have the same bug. This is tracked in the schemas issue.
  • Not changed: the "Request validation failed" wording is still used when a response event fails validation. That message is shared by 5 methods and is out of scope here.

Tests

  • task lint (black, isort, mypy): passing
  • task test: 53 passed, 1 xfailed (pre-existing)

no docs ticket: internal-only refactor - this is a bug fix with no new public behavior. The documented create_message_stream usage now works as described.

Closes #171

Generated with Claude Code

@edenreich

Copy link
Copy Markdown
Contributor

@claude can you verify with the official OpenAI spec if this field should be nullable, if it's can you make the change or file a refactoring to inference-gateway/schemas?

@inference-gateway-maintainer

inference-gateway-maintainer Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Claude finished @edenreich's task in 50s —— View job


Verifying stop_reason nullability

  • Check the PR diff
  • Check the official upstream spec for stop_reason
  • Check inference-gateway/schemas and the existing follow-up issue
  • Add the findings to the schemas issue

Result: yes, it should be nullable. The PR is correct as is.

One note first: MessagesResponse.stop_reason comes from the Anthropic Messages API, not OpenAI, so I checked it against Anthropic's official spec. The Message type in anthropics/anthropic-sdk-python is generated from Anthropic's OpenAPI spec. It declares stop_reason: Optional[StopReason] with this doc:

In non-streaming mode this value is always non-null. In streaming mode, it is null in the message_start event and non-null otherwise.

That matches this PR exactly: nullable: true and still in required. I did not push any code changes because none are needed.

OpenAI equivalent

The OpenAI field that does the same job is finish_reason. The official ChatCompletionChunk.Choice.finish_reason is also Optional[...], and it is null on every chunk except the last. In this repo, ChatCompletionStreamChoice.finish_reason (inference_gateway/models.py:2646) is still required and non-nullable, so it has the same bug. That is already tracked upstream in inference-gateway/schemas#328, so I left it out of this PR.

Upstream (inference-gateway/schemas)

The upstream issue already exists (inference-gateway/schemas#327), so I didn't open a duplicate. I commented there with the official-source evidence and one more gap I found: the official Anthropic StopReason enum includes model_context_window_exceeded, which our enum is missing. A response with that value would also fail validation, so it should go into the same upstream fix.

Not done

  • I didn't add model_context_window_exceeded to this PR. It's a separate enum gap, and it belongs in the upstream spec fix.
  • Until #327 lands, running task generate will still revert this local fix.

@edenreich
edenreich merged commit cb9440e into main Nov 17, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-171-20261117-1939 branch November 17, 2026 20:11
inference-gateway-releaser Bot added a commit that referenced this pull request Nov 17, 2026
## [0.26.0](v0.25.0...v0.26.0) (2026-11-17)

### ✨ Features

* sync schemas v1.14.1 and accept standard OpenAI stream chunks ([#182](#182)) ([d4e2634](d4e2634)), closes [#181](#181)

### 🐛 Bug Fixes

* allow null stop_reason in Messages stream message_start ([#179](#179)) ([cb9440e](cb9440e))
* read stream chunks as JSON in chat examples ([#178](#178)) ([fca4630](fca4630)), closes [#172](#172)

### 👷 CI

* align Dependabot schedule and grouping ([#169](#169)) ([3f5ad19](3f5ad19))
* set up orchestrators from repos.yaml ([#170](#170)) ([bffe983](bffe983))

### 📚 Documentation

* add audio example for speech, sfx and music ([#176](#176)) ([c1f04d0](c1f04d0))
* add Responses API example and README section ([#180](#180)) ([ec0e02a](ec0e02a))
* sync examples and README with gateway config ([#177](#177)) ([ee0999d](ee0999d))

### 🔧 Miscellaneous

* **deps:** bump flox dependencies to latest ([#167](#167)) ([55f32c5](55f32c5))
* **deps:** bump flox dependencies to latest ([#168](#168)) ([de020e9](de020e9))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.26.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] create_message_stream raises on the Anthropic message_start event (stop_reason is null)

1 participant