Skip to content
Closed
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
30 changes: 26 additions & 4 deletions src/llama_stack_client/_decoders/jsonl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@ class JSONLDecoder(Generic[_T]):
into a given type.
"""

http_response: httpx.Response | None
http_response: httpx.Response
"""The HTTP response this decoder was constructed from"""

def __init__(
self, *, raw_iterator: Iterator[bytes], line_type: type[_T], http_response: httpx.Response | None
self,
*,
raw_iterator: Iterator[bytes],
line_type: type[_T],
http_response: httpx.Response,
) -> None:
super().__init__()
self.http_response = http_response
self._raw_iterator = raw_iterator
self._line_type = line_type
self._iterator = self.__decode__()

def close(self) -> None:
"""Close the response body stream.

This is called automatically if you consume the entire stream.
"""
self.http_response.close()

def __decode__(self) -> Iterator[_T]:
buf = b""
for chunk in self._raw_iterator:
Expand Down Expand Up @@ -63,17 +74,28 @@ class AsyncJSONLDecoder(Generic[_T]):
into a given type.
"""

http_response: httpx.Response | None
http_response: httpx.Response

def __init__(
self, *, raw_iterator: AsyncIterator[bytes], line_type: type[_T], http_response: httpx.Response | None
self,
*,
raw_iterator: AsyncIterator[bytes],
line_type: type[_T],
http_response: httpx.Response,
) -> None:
super().__init__()
self.http_response = http_response
self._raw_iterator = raw_iterator
self._line_type = line_type
self._iterator = self.__decode__()

async def close(self) -> None:
"""Close the response body stream.

This is called automatically if you consume the entire stream.
"""
await self.http_response.aclose()

async def __decode__(self) -> AsyncIterator[_T]:
buf = b""
async for chunk in self._raw_iterator:
Expand Down
8 changes: 7 additions & 1 deletion src/llama_stack_client/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,16 @@ def construct_type(*, value: object, type_: object) -> object:

If the given value does not match the expected type then it is returned as-is.
"""

# store a reference to the original type we were given before we extract any inner
# types so that we can properly resolve forward references in `TypeAliasType` annotations
original_type = None

# we allow `object` as the input type because otherwise, passing things like
# `Literal['value']` will be reported as a type error by type checkers
type_ = cast("type[object]", type_)
if is_type_alias_type(type_):
original_type = type_ # type: ignore[unreachable]
type_ = type_.__value__ # type: ignore[unreachable]

# unwrap `Annotated[T, ...]` -> `T`
Expand All @@ -446,7 +452,7 @@ def construct_type(*, value: object, type_: object) -> object:

if is_union(origin):
try:
return validate_type(type_=cast("type[object]", type_), value=value)
return validate_type(type_=cast("type[object]", original_type or type_), value=value)
except Exception:
pass

Expand Down
4 changes: 2 additions & 2 deletions src/llama_stack_client/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
return cast(
R,
cast("type[JSONLDecoder[Any]]", cast_to)(
raw_iterator=self.http_response.iter_bytes(chunk_size=4096),
raw_iterator=self.http_response.iter_bytes(chunk_size=64),
line_type=extract_type_arg(cast_to, 0),
http_response=self.http_response,
),
Expand All @@ -154,7 +154,7 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
return cast(
R,
cast("type[AsyncJSONLDecoder[Any]]", cast_to)(
raw_iterator=self.http_response.aiter_bytes(chunk_size=4096),
raw_iterator=self.http_response.aiter_bytes(chunk_size=64),
line_type=extract_type_arg(cast_to, 0),
http_response=self.http_response,
),
Expand Down
12 changes: 11 additions & 1 deletion src/llama_stack_client/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
is_annotated_type,
strip_annotated_type,
)
from .._compat import model_dump, is_typeddict
from .._compat import get_origin, model_dump, is_typeddict

_T = TypeVar("_T")

Expand Down Expand Up @@ -164,9 +164,14 @@ def _transform_recursive(
inner_type = annotation

stripped_type = strip_annotated_type(inner_type)
origin = get_origin(stripped_type) or stripped_type
if is_typeddict(stripped_type) and is_mapping(data):
return _transform_typeddict(data, stripped_type)

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
Expand Down Expand Up @@ -307,9 +312,14 @@ async def _async_transform_recursive(
inner_type = annotation

stripped_type = strip_annotated_type(inner_type)
origin = get_origin(stripped_type) or stripped_type
if is_typeddict(stripped_type) and is_mapping(data):
return await _async_transform_typeddict(data, stripped_type)

if origin == dict and is_mapping(data):
items_type = get_args(stripped_type)[1]
return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}

if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
Expand Down
8 changes: 4 additions & 4 deletions src/llama_stack_client/lib/cli/llama_stack_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ def cli(ctx, endpoint: str, api_key: str, config: str | None):

# Register all subcommands
cli.add_command(models, "models")
cli.add_command(vector_dbs, "vector_dbs")
cli.add_command(vector_dbs, "vector-dbs")
cli.add_command(shields, "shields")
cli.add_command(eval_tasks, "eval_tasks")
cli.add_command(eval_tasks, "eval-tasks")
cli.add_command(providers, "providers")
cli.add_command(datasets, "datasets")
cli.add_command(configure, "configure")
cli.add_command(scoring_functions, "scoring_functions")
cli.add_command(scoring_functions, "scoring-functions")
cli.add_command(eval, "eval")
cli.add_command(inference, "inference")
cli.add_command(post_training, "post_training")
cli.add_command(post_training, "post-training")
cli.add_command(inspect, "inspect")
cli.add_command(toolgroups, "toolgroups")

Expand Down
32 changes: 16 additions & 16 deletions src/llama_stack_client/resources/eval_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def with_streaming_response(self) -> EvalTasksResourceWithStreamingResponse:

def retrieve(
self,
eval_task_id: str,
task_id: str,
*,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
Expand All @@ -69,10 +69,10 @@ def retrieve(

timeout: Override the client-level default timeout for this request, in seconds
"""
if not eval_task_id:
raise ValueError(f"Expected a non-empty value for `eval_task_id` but received {eval_task_id!r}")
if not task_id:
raise ValueError(f"Expected a non-empty value for `task_id` but received {task_id!r}")
return self._get(
f"/v1/eval-tasks/{eval_task_id}",
f"/v1/eval/tasks/{task_id}",
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand All @@ -90,7 +90,7 @@ def list(
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> EvalTaskListResponse:
return self._get(
"/v1/eval-tasks",
"/v1/eval/tasks",
options=make_request_options(
extra_headers=extra_headers,
extra_query=extra_query,
Expand All @@ -105,8 +105,8 @@ def register(
self,
*,
dataset_id: str,
eval_task_id: str,
scoring_functions: List[str],
task_id: str,
metadata: Dict[str, Union[bool, float, str, Iterable[object], object, None]] | NotGiven = NOT_GIVEN,
provider_eval_task_id: str | NotGiven = NOT_GIVEN,
provider_id: str | NotGiven = NOT_GIVEN,
Expand All @@ -129,12 +129,12 @@ def register(
"""
extra_headers = {"Accept": "*/*", **(extra_headers or {})}
return self._post(
"/v1/eval-tasks",
"/v1/eval/tasks",
body=maybe_transform(
{
"dataset_id": dataset_id,
"eval_task_id": eval_task_id,
"scoring_functions": scoring_functions,
"task_id": task_id,
"metadata": metadata,
"provider_eval_task_id": provider_eval_task_id,
"provider_id": provider_id,
Expand Down Expand Up @@ -170,7 +170,7 @@ def with_streaming_response(self) -> AsyncEvalTasksResourceWithStreamingResponse

async def retrieve(
self,
eval_task_id: str,
task_id: str,
*,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
Expand All @@ -189,10 +189,10 @@ async def retrieve(

timeout: Override the client-level default timeout for this request, in seconds
"""
if not eval_task_id:
raise ValueError(f"Expected a non-empty value for `eval_task_id` but received {eval_task_id!r}")
if not task_id:
raise ValueError(f"Expected a non-empty value for `task_id` but received {task_id!r}")
return await self._get(
f"/v1/eval-tasks/{eval_task_id}",
f"/v1/eval/tasks/{task_id}",
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand All @@ -210,7 +210,7 @@ async def list(
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> EvalTaskListResponse:
return await self._get(
"/v1/eval-tasks",
"/v1/eval/tasks",
options=make_request_options(
extra_headers=extra_headers,
extra_query=extra_query,
Expand All @@ -225,8 +225,8 @@ async def register(
self,
*,
dataset_id: str,
eval_task_id: str,
scoring_functions: List[str],
task_id: str,
metadata: Dict[str, Union[bool, float, str, Iterable[object], object, None]] | NotGiven = NOT_GIVEN,
provider_eval_task_id: str | NotGiven = NOT_GIVEN,
provider_id: str | NotGiven = NOT_GIVEN,
Expand All @@ -249,12 +249,12 @@ async def register(
"""
extra_headers = {"Accept": "*/*", **(extra_headers or {})}
return await self._post(
"/v1/eval-tasks",
"/v1/eval/tasks",
body=await async_maybe_transform(
{
"dataset_id": dataset_id,
"eval_task_id": eval_task_id,
"scoring_functions": scoring_functions,
"task_id": task_id,
"metadata": metadata,
"provider_eval_task_id": provider_eval_task_id,
"provider_id": provider_id,
Expand Down
4 changes: 2 additions & 2 deletions src/llama_stack_client/types/eval_task_register_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
class EvalTaskRegisterParams(TypedDict, total=False):
dataset_id: Required[str]

eval_task_id: Required[str]

scoring_functions: Required[List[str]]

task_id: Required[str]

metadata: Dict[str, Union[bool, float, str, Iterable[object], object, None]]

provider_eval_task_id: str
Expand Down
Loading