Skip to content

Add a more explicit error when no parts returned #180

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

Merged
merged 2 commits into from
Jan 30, 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
6 changes: 2 additions & 4 deletions google/generativeai/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def embed_content(
task_type: EmbeddingTaskTypeOptions | None = None,
title: str | None = None,
client: glm.GenerativeServiceClient | None = None,
) -> text_types.EmbeddingDict:
...
) -> text_types.EmbeddingDict: ...


@overload
Expand All @@ -104,8 +103,7 @@ def embed_content(
task_type: EmbeddingTaskTypeOptions | None = None,
title: str | None = None,
client: glm.GenerativeServiceClient | None = None,
) -> text_types.BatchEmbeddingDict:
...
) -> text_types.BatchEmbeddingDict: ...


def embed_content(
Expand Down
6 changes: 2 additions & 4 deletions google/generativeai/notebook/lib/llmfn_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ def __len__(self) -> int:

# Needed for Sequence[LLMFnOutputEntry].
@overload
def __getitem__(self, x: int) -> LLMFnOutputEntry:
...
def __getitem__(self, x: int) -> LLMFnOutputEntry: ...

@overload
def __getitem__(self, x: slice) -> Sequence[LLMFnOutputEntry]:
...
def __getitem__(self, x: slice) -> Sequence[LLMFnOutputEntry]: ...

def __getitem__(self, x: int | slice) -> LLMFnOutputEntry | Sequence[LLMFnOutputEntry]:
return self._outputs.__getitem__(x)
Expand Down
4 changes: 1 addition & 3 deletions google/generativeai/notebook/magics_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def parse_line(
) -> tuple[parsed_args_lib.ParsedArgs, parsed_args_lib.PostProcessingTokens]:
return cmd_line_parser.CmdLineParser().parse_line(line, placeholders)

def _get_handler(
self, line: str, placeholders: AbstractSet[str]
) -> tuple[
def _get_handler(self, line: str, placeholders: AbstractSet[str]) -> tuple[
command.Command,
parsed_args_lib.ParsedArgs,
Sequence[post_process_utils.ParsedPostProcessExpr],
Expand Down
6 changes: 2 additions & 4 deletions google/generativeai/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,15 @@ def generate_embeddings(
model: model_types.BaseModelNameOptions,
text: str,
client: glm.TextServiceClient = None,
) -> text_types.EmbeddingDict:
...
) -> text_types.EmbeddingDict: ...


@overload
def generate_embeddings(
model: model_types.BaseModelNameOptions,
text: Sequence[str],
client: glm.TextServiceClient = None,
) -> text_types.BatchEmbeddingDict:
...
) -> text_types.BatchEmbeddingDict: ...


def generate_embeddings(
Expand Down
15 changes: 12 additions & 3 deletions google/generativeai/types/generation_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,11 @@ class BaseGenerateContentResponse:
def __init__(
self,
done: bool,
iterator: None
| Iterable[glm.GenerateContentResponse]
| AsyncIterable[glm.GenerateContentResponse],
iterator: (
None
| Iterable[glm.GenerateContentResponse]
| AsyncIterable[glm.GenerateContentResponse]
),
result: glm.GenerateContentResponse,
chunks: Iterable[glm.GenerateContentResponse],
):
Expand Down Expand Up @@ -324,6 +326,13 @@ def text(self):
ValueError: If the candidate list or parts list does not contain exactly one entry.
"""
parts = self.parts
if not parts:
raise ValueError(
"The `response.text` quick accessor only works when the response contains a valid "
"`Part`, but none was returned. Check the `candidate.safety_ratings` to see if the "
"response was blocked."
)

if len(parts) != 1 or "text" not in parts[0]:
raise ValueError(
"The `response.text` quick accessor only works for "
Expand Down