Skip to content

fix(api): cast replica to int to prevent TypeError on multi-replica launch - #5220

Merged
qinxuye merged 3 commits into
xorbitsai:mainfrom
m199369309:fix/replica-string-typeerror
Jul 22, 2026
Merged

fix(api): cast replica to int to prevent TypeError on multi-replica launch#5220
qinxuye merged 3 commits into
xorbitsai:mainfrom
m199369309:fix/replica-string-typeerror

Conversation

@m199369309

Copy link
Copy Markdown
Collaborator

Summary

Fix a TypeError: \x27str\x27 object cannot be interpreted as an integer when launching a model with replica > 1 via the REST API.

Root cause

The frontend submits replica as a string in the JSON body (e.g. "replica": "2"). payload.get("replica", 1) returns the raw string, and list(range("2")) raises TypeError.

Single-replica deployments are unaffected because the frontend usually omits the field entirely, so the default 1 (int) is used.

Fix

Wrap payload.get("replica", 1) with int() in both launch_model() and launch_model_by_version():

# before
replica = payload.get("replica", 1)

# after
replica = int(payload.get("replica", 1))

This handles both integer (int(1) -> 1) and string (int("2") -> 2) inputs safely.

Backward compatibility

✅ Fully compatible — int(1) is a no-op, int("2") produces the correct integer.

🤖 Generated with Claude Code

…aunch

When the frontend submits 'replica' as a string (e.g. '2') in the JSON
body, payload.get() returns the raw string, and list(range('2')) raises
TypeError: 'str' object cannot be interpreted as an integer.

Wrap the value with int() so both integer and string inputs are handled
correctly. This affects both launch_model() and launch_model_by_version().

Co-Authored-By: Claude <noreply@anthropic.com>
@XprobeBot XprobeBot added the bug Something isn't working label Jul 21, 2026
@XprobeBot XprobeBot added this to the v3.x milestone Jul 21, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request casts the replica payload parameter to an integer in both the launch_model and launch_model_by_version endpoints. The review comments point out that if replica is explicitly passed as null in the payload, int(None) will raise a TypeError and crash the request with a 500 Internal Server Error. Additionally, if replica is less than 1, it can cause a scheduler crash. The reviewer suggests safely parsing and validating the replica value, raising a 400 Bad Request if it is invalid.

Comment thread xinference/api/restful_api.py Outdated
Comment thread xinference/api/restful_api.py Outdated
Apply the same int() validation and < 1 guard to launch_model_by_version
that was already applied to launch_model.

Previously, passing "replica": null or "replica": 0 via the REST API
would trigger an unhandled TypeError (500 crash) or a scheduler crash
respectively. Now both endpoints return HTTP 400 with a clear error message.

Co-Authored-By: Claude <noreply@anthropic.com>
Comment thread xinference/api/restful_api.py Outdated
- Add _validate_replica() helper that checks the raw JSON type before int()
- Reject bool (True/False) and float (including inf/nan) with 400
- Catch OverflowError in addition to TypeError/ValueError
- Apply to both launch_model and launch_model_by_version
- Add focused unit tests for all validation paths

Addresses review feedback from qinxuye on PR xorbitsai#5220.

Co-Authored-By: Claude <noreply@anthropic.com>

@qinxuye qinxuye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qinxuye
qinxuye merged commit 1a011c3 into xorbitsai:main Jul 22, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants