Skip to content

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

Closed

Description

Summary

create_message_stream raises InferenceGatewayValidationError on the first event of an Anthropic stream, so the streaming half of examples/messages/main.py, the "Streaming Message" snippet in examples/messages/README.md and the create_message_stream snippet in the root README.md cannot work. The gateway relays the Anthropic SSE envelope verbatim (see the MessagesHandler comment in api/messages.go of inference-gateway/inference-gateway), and the Anthropic message_start event carries "stop_reason": null. _process_messages_stream (inference_gateway/client.py:580-585) validates every payload as MessagesStreamEvent, whose message field is a MessagesResponse with a required, non-nullable stop_reason literal (inference_gateway/models.py:2079-2081, generated from openapi.yaml:4936-4961). The unit test does not catch it because its fixture sends "stop_reason":"end_turn" inside message_start (tests/test_client.py:1010-1013), which is not what the provider sends. The models are generated, so the fix belongs in inference-gateway/schemas (allow a null stop_reason on the message carried by message_start), followed by task generate and a test fixture that uses the real event shape. Reproduced at v0.25.0 with a mocked HTTP response that uses the event shape from the Anthropic streaming guide, not against a live gateway.

Steps to Reproduce

  1. Install the SDK from main (v0.25.0) with pip install -e ".[dev]".
  2. Save and run this script. It feeds the client a message_start event in the shape the Anthropic streaming guide documents:
from unittest.mock import Mock, patch

from inference_gateway import InferenceGatewayClient, MessagesMessage

lines = [
    b"event: message_start",
    b'data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","content":[],"model":"claude-sonnet-5","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":25,"output_tokens":1}}}',
    b"event: content_block_delta",
    b'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}',
]
response = Mock(status_code=200)
response.raise_for_status.return_value = None
response.iter_lines.return_value = lines

client = InferenceGatewayClient("http://localhost:8080/v1")
with patch("requests.Session.post", return_value=response):
    for event in client.create_message_stream(
        model="anthropic/claude-sonnet-5",
        messages=[MessagesMessage(role="user", content="Hi")],
        max_tokens=50,
    ):
        print("event:", event.type)
  1. Observe the exception raised before any event is yielded.

Expected Behavior

The generator yields message_start and then content_block_delta as typed MessagesStreamEvent objects, and examples/messages/main.py prints the streamed text.

Actual Behavior

The generator raises before yielding anything:

InferenceGatewayValidationError: Request validation failed: 1 validation error for MessagesStreamEvent
message.stop_reason
  Input should be 'end_turn', 'max_tokens', 'stop_sequence', 'tool_use', 'pause_turn' or 'refusal' [type=literal_error, input_value=None, input_type=NoneType]

With the same stream, examples/messages/main.py prints Streaming Error: Request validation failed: ... and no assistant text. The message also says "Request validation failed" although the request was valid and it is a response event that failed validation (inference_gateway/client.py:1274-1275).

Activity

  1. edenreich commented on Nov 17, 2026

    @edenreich
    Contributor

    @claude can you fix this?

  2. inference-gateway-maintainer commented on Nov 17, 2026

    @inference-gateway-maintainer
    ContributorAuthor

    Claude finished @edenreich's task in 2m 27s —— View job • fix/issue-171-20261117-1939 • [Create PR ➔](https://github.com/inference-gateway/python-sdk/compare/main...fix/issue-171-20261117-1939?quick_pull=1&title=Issue%20%23171%3A%20Changes%20from%20Claude&body=This%20PR%20addresses%20issue%20%23171%0A%0AGenerated%20with%20%5BClaude%20Code%5D(https%3A%2F%2Fclaude.ai%2Fcode)


    Fixed in #179

    • Track issue on Roadmap board (In progress, then QA)
    • Gather context
    • Implement fix
    • Fix test fixture to use real message_start shape
    • Run lint + tests
    • Commit and open PR

    What changed

    • openapi.yaml: MessagesResponse.stop_reason is now nullable: true. It is still required.
    • inference_gateway/models.py: stop_reason is now Literal[...] | None.
    • tests/test_client.py: test_create_message_stream now uses the real message_start shape ("stop_reason": null, "stop_sequence": null) and checks that stop_reason is None.

    Checks: task lint passes (black, isort, mypy). task test gives 53 passed and 1 xfailed - that xfail was already there before this change.

    Things to know

    • I edited the generated model by hand. The sandbox needed approval to run datamodel-codegen, and task generate would download the upstream spec and overwrite the local edit. The edit follows how the generator already writes other required nullable fields. Rerunning the codegen command without the download step would confirm there is no diff.
    • The upstream schema is not fixed yet. Until it is, the next task generate will undo this fix. I opened [BUG] MessagesResponse.stop_reason must be nullable for streaming message_start schemas#327 for it. The other SDKs built from the same schema probably have the same bug.
    • Small typing change: MessagesResponse.stop_reason can now be None. Code checked with mypy against the old type may need a None check. Non-streaming responses behave the same as before.
    • Not changed: when a response event fails validation, the error still says "Request validation failed". Five methods use that message, so I left it out of this fix.
    • No docs ticket, since this is a bug fix with no new public behavior.
      · Branch
  3. moved this from Todo to In progress in 2026on Nov 17, 2026
  4. moved this from In progress to QA in 2026on Nov 17, 2026
  5. moved this from QA to Done in 2026on Nov 17, 2026
  6. inference-gateway-releaser commented on Nov 17, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.26.0 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugreleased

    Type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions