Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
golang 1.24.2
python 3.13.2
31 changes: 26 additions & 5 deletions python/cog/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,38 @@ def validate_input_type(


def get_input_create_model_kwargs(signature: inspect.Signature) -> Dict[str, Any]:
create_model_kwargs = {}
create_model_kwargs: Dict[str, Any] = {
"__base__": BaseInput,
"__config__": None,
}

order = 0

for name, parameter in signature.parameters.items():
InputType = parameter.annotation

if parameter.kind == inspect.Parameter.VAR_POSITIONAL:
raise TypeError(f"Unsupported variadic positional parameter *{name}.")

if parameter.kind == inspect.Parameter.VAR_KEYWORD:
if order != 0:
raise TypeError(f"Unsupported variadic keyword parameter **{name}")

class ExtraKeywordInput(BaseInput):
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment explaining the purpose of the 'ExtraKeywordInput' class to clarify its role in handling exclusive **kwargs inputs.

Copilot uses AI. Check for mistakes.
if PYDANTIC_V2:
model_config = pydantic.ConfigDict(extra="allow")
else:

class Config:
extra = "allow"

create_model_kwargs["__base__"] = ExtraKeywordInput
name = "__pydantic_extra__"
InputType = Dict[str, Any]

create_model_kwargs[name] = (InputType, Input())
continue

validate_input_type(InputType, name)

# if no default is specified, create an empty, required input
Expand Down Expand Up @@ -325,8 +350,6 @@ class Input(BaseModel):

return create_model(
"Input",
__config__=None,
__base__=BaseInput,
__module__=__name__,
__validators__=None,
**get_input_create_model_kwargs(signature),
Expand Down Expand Up @@ -431,8 +454,6 @@ class TrainingInput(BaseModel):

return create_model(
"TrainingInput",
__config__=None,
__base__=BaseInput,
__module__=__name__,
__validators__=None,
**get_input_create_model_kwargs(signature),
Expand Down
8 changes: 8 additions & 0 deletions python/tests/server/fixtures/input_kwargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Dict

from cog import BasePredictor


class Predictor(BasePredictor):
def predict(self, **kwargs) -> Dict:
return kwargs
12 changes: 12 additions & 0 deletions python/tests/server/test_http_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def test_empty_input(client, match):
assert resp.json() == match({"status": "succeeded", "output": "foobar"})


@uses_predictor("input_kwargs")
def test_kwargs_input(client, match):
"""Check we support kwargs input fields"""
input = {"animal": "giraffe", "no": 5}
resp = client.post("/predictions", json={"input": input})
assert resp.json() == match({"status": "succeeded"})
assert resp.status_code == 200

result = resp.json()["output"]
assert result == input


@uses_predictor("input_integer")
def test_good_int_input(client, match):
resp = client.post("/predictions", json={"input": {"num": 3}})
Expand Down