ultralytics/yolov5 · error · RuntimeError
Cannot specify args and kwargs at the same time
Error message
Cannot specify args and kwargs at the same time
What it means
Raised by TritonClient._create_inputs (utils/triton.py) when the model call provides BOTH positional and keyword arguments. The input-building code must iterate either positionally-matched placeholders or kwargs keyed by input name; mixing the two makes tensor-to-input assignment ambiguous, so it rejects the call.
Source
Thrown at utils/triton.py:66
Parameters can be provided via args or kwargs. args, if provided, are assumed to match the order of inputs of
the model. kwargs are matched with the model input names.
"""
inputs = self._create_inputs(*args, **kwargs)
response = self.client.infer(model_name=self.model_name, inputs=inputs)
result = []
for output in self.metadata["outputs"]:
tensor = torch.as_tensor(response.as_numpy(output["name"]))
result.append(tensor)
return result[0] if len(result) == 1 else result
def _create_inputs(self, *args, **kwargs):
"""Creates input tensors from args or kwargs, not both; raises error if none or both are provided."""
args_len, kwargs_len = len(args), len(kwargs)
if not args_len and not kwargs_len:
raise RuntimeError("No inputs provided.")
if args_len and kwargs_len:
raise RuntimeError("Cannot specify args and kwargs at the same time")
placeholders = self._create_input_placeholders_fn()
if args_len:
if args_len != len(placeholders):
raise RuntimeError(f"Expected {len(placeholders)} inputs, got {args_len}.")
for input, value in zip(placeholders, args):
input.set_data_from_numpy(value.cpu().numpy())
else:
for input in placeholders:
value = kwargs[input.name]
input.set_data_from_numpy(value.cpu().numpy())
return placeholders
View on GitHub (pinned to 20d1d78a08)
Solutions
- Use one style exclusively: model(images) positional, or model(images=images) keyword (the kwarg name must equal the Triton input name, e.g. 'images').
- In dispatch layers, normalize the payload to either a tuple or a dict before calling: model(**payload) or model(*payload), never both.
- Add a request-schema check that rejects payloads containing both array-form and object-form inputs.
Example fix
# before outputs = model(img, images=img) # after outputs = model(img) # or outputs = model(images=img)
Defensive patterns
Strategy: validation
Validate before calling
if args and kwargs:
raise ValueError("Pass inputs as positional args OR keyword args, not both.")
outputs = triton_model(*(args or ()), **(kwargs or {})) Type guard
def uses_single_input_style(args: tuple, kwargs: dict) -> bool:
"""TritonClient accepts exactly one of positional or keyword inputs."""
return not (len(args) > 0 and len(kwargs) > 0) Try / catch
try:
outputs = triton_model(inputs)
except RuntimeError as e:
if "args and kwargs" in str(e):
outputs = triton_model(inputs) # retry with a single, normalized input style
else:
raise Prevention
- Normalize request payloads to one canonical form (list or dict) at the service boundary.
- Avoid blanket model(*args, **kwargs) forwarding in dispatch layers.
- Pin a code-style rule: Triton calls use keyword inputs keyed by the model's declared input names.
When it happens
Trigger: Calling the Triton model wrapper as model(tensor_a, images=tensor_b) — any call where len(args) > 0 and len(kwargs) > 0 simultaneously.
Common situations: Generic dispatch code that does model(*args, **kwargs) and receives a payload containing both a list and a dict; incremental refactoring from positional to keyword calls leaving both in place; copy-pasted examples combining the two styles.
Related errors
- No inputs provided.
- Expected {len(placeholders)} inputs, got {args_len}.
- Source path '{source}' does not exist
- TensorRT engine deserialization failed. Re-export the engine
- ERROR: YOLOv5 TF.js inference is not supported
AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15).
Data as JSON: /api/errors/739cf5faaadcdd3d.
Report an issue: GitHub.