Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set content type from annotation #4416

Merged
merged 1 commit into from
Jan 18, 2024
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
8 changes: 1 addition & 7 deletions src/_bentoml_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class APIMethod(t.Generic[P, R]):
name: str = attrs.field()
input_spec: type[IODescriptor] = attrs.field()
output_spec: type[IODescriptor] = attrs.field()
media_type: str | None = None
batchable: bool = False
batch_dim: tuple[int, int] = attrs.field(
default=(0, 0), converter=lambda x: (x, x) if not isinstance(x, tuple) else x
Expand Down Expand Up @@ -84,9 +83,7 @@ def default_output_spec(self) -> type[IODescriptor]:
return IODescriptor.from_output(self.func)

def __attrs_post_init__(self) -> None:
if self.media_type:
self.output_spec.media_type = self.media_type
elif self.is_stream:
if self.is_stream and not self.output_spec.media_type:
self.output_spec.media_type = DEFAULT_STREAM_MEDIA_TYPE

@t.overload
Expand Down Expand Up @@ -232,7 +229,6 @@ def api(
name: str | None = ...,
input_spec: type[IODescriptor] | None = ...,
output_spec: type[IODescriptor] | None = ...,
media_type: str | None = ...,
batchable: bool = ...,
batch_dim: int | tuple[int, int] = ...,
max_batch_size: int = ...,
Expand All @@ -248,7 +244,6 @@ def api(
name: str | None = None,
input_spec: type[IODescriptor] | None = None,
output_spec: type[IODescriptor] | None = None,
media_type: str | None = None,
batchable: bool = False,
batch_dim: int | tuple[int, int] = 0,
max_batch_size: int = 100,
Expand All @@ -259,7 +254,6 @@ def api(
):
def wrapper(func: t.Callable[t.Concatenate[t.Any, P], R]) -> APIMethod[P, R]:
params: dict[str, t.Any] = {
"media_type": media_type,
"batchable": batchable,
"batch_dim": batch_dim,
"max_batch_size": max_batch_size,
Expand Down
17 changes: 13 additions & 4 deletions src/_bentoml_sdk/io_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydantic import Field
from pydantic import RootModel
from pydantic import create_model
from pydantic._internal._typing_extra import is_annotated
from typing_extensions import get_args

from bentoml._internal.service.openapi.specification import Schema
Expand All @@ -19,6 +20,7 @@
from .typing_utils import is_iterator_type
from .typing_utils import is_list_type
from .typing_utils import is_union_type
from .validators import ContentType

if t.TYPE_CHECKING:
from starlette.requests import Request
Expand Down Expand Up @@ -84,8 +86,6 @@ def mime_type(cls) -> str:

@classmethod
def __pydantic_init_subclass__(cls) -> None:
from pydantic._internal._typing_extra import is_annotated

cls.multipart_fields = []
for k, field in cls.model_fields.items():
annotation = field.annotation
Expand Down Expand Up @@ -288,11 +288,20 @@ def from_output(cls, func: t.Callable[..., t.Any]) -> type[IODescriptor]:
return_annotation = eval_type_lenient(
signature.return_annotation, global_ns, None
)

media_type: str | None = None
if is_iterator_type(return_annotation):
return_annotation = get_args(return_annotation)[0]
elif is_annotated(return_annotation):
content_type = next(
(a for a in get_args(return_annotation) if isinstance(a, ContentType)),
None,
)
if content_type is not None:
media_type = content_type.content_type
try:
return ensure_io_descriptor(return_annotation)
output = ensure_io_descriptor(return_annotation)
output.media_type = media_type
return output
except (ValueError, TypeError) as e:
raise TypeError(
f"Unable to infer the output spec for function {func}, "
Expand Down
10 changes: 1 addition & 9 deletions src/bentoml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,7 @@
validators = _LazyLoader("bentoml.validators", globals(), "bentoml.validators")
del _LazyLoader

_NEW_SDK_ATTRS = [
"service",
"runner_service",
"api",
"depends",
"ContentType",
"Dtype",
"Shape",
]
_NEW_SDK_ATTRS = ["service", "runner_service", "api", "depends"]
Copy link
Member

Choose a reason for hiding this comment

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

Where do we expose ContentType now? @frostming

Copy link
Contributor Author

Choose a reason for hiding this comment

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

bentoml.validators.ContentType

_NEW_CLIENTS = ["SyncHTTPClient", "AsyncHTTPClient"]

def __getattr__(name: str) -> Any:
Expand Down
Loading