Repository navigation
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
- Install the SDK from
main(v0.25.0) withpip install -e ".[dev]". - Save and run this script. It feeds the client a
message_startevent 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)- 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
edenreich commentedon Nov 17, 2026
@claude can you fix this?
inference-gateway-maintainer commentedon Nov 17, 2026
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
message_startshapeWhat changed
openapi.yaml:MessagesResponse.stop_reasonis nownullable: true. It is still required.inference_gateway/models.py:stop_reasonis nowLiteral[...] | None.tests/test_client.py:test_create_message_streamnow uses the realmessage_startshape ("stop_reason": null,"stop_sequence": null) and checks thatstop_reason is None.Checks:
task lintpasses (black, isort, mypy).task testgives 53 passed and 1 xfailed - that xfail was already there before this change.Things to know
datamodel-codegen, andtask generatewould 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.task generatewill 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.MessagesResponse.stop_reasoncan now beNone. Code checked with mypy against the old type may need aNonecheck. Non-streaming responses behave the same as before.· Branch
inference-gateway-releaser commentedon Nov 17, 2026
🎉 This issue has been resolved in version 0.26.0 🎉
The release is available on GitHub release
Your semantic-release bot 📦🚀