Skip to content

feat: enhance error handling #76

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 3 commits into from
May 29, 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
45 changes: 32 additions & 13 deletions literalai/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def raise_error(error):
raise Exception(error)

variables = self._prepare_variables(variables)

with httpx.Client() as client:
response = client.post(
self.graphql_endpoint,
Expand All @@ -191,8 +190,10 @@ def raise_error(error):
timeout=10,
)

if response.status_code >= 400:
raise_error(response.text)
try:
response.raise_for_status()
except httpx.HTTPStatusError:
raise_error(f"Failed to {description}: {response.text}")

json = response.json()

Expand Down Expand Up @@ -232,7 +233,13 @@ def make_rest_call(self, subpath: str, body: Dict[str, Any]) -> Dict:
timeout=20,
)

response.raise_for_status()
try:
response.raise_for_status()
except httpx.HTTPStatusError:
message = f"Failed to call {subpath}: {response.text}"
logger.error(message)
raise Exception(message)

json = response.json()

return json
Expand Down Expand Up @@ -1217,7 +1224,9 @@ def add_generation_to_dataset(

# Prompt API

def get_or_create_prompt_lineage(self, name: str, description: Optional[str] = None):
def get_or_create_prompt_lineage(
self, name: str, description: Optional[str] = None
):
"""
Creates a prompt lineage with the specified name and optional description.
If the prompt lineage with that name already exists, it is returned.
Expand Down Expand Up @@ -1298,7 +1307,7 @@ def get_prompt(
return self.gql_helper(*get_prompt_helper(self, name=name, version=version))
else:
raise ValueError("Either the `id` or the `name` must be provided.")

def promote_prompt(self, name: str, version: int) -> str:
"""
Promotes the prompt with name to target version.
Expand All @@ -1315,7 +1324,6 @@ def promote_prompt(self, name: str, version: int) -> str:

return self.gql_helper(*promote_prompt_helper(lineage_id, version))


# Misc API

def get_my_project_id(self):
Expand Down Expand Up @@ -1364,8 +1372,10 @@ def raise_error(error):
timeout=10,
)

if response.status_code >= 400:
raise_error(response.text)
try:
response.raise_for_status()
except httpx.HTTPStatusError:
raise_error(f"Failed to {description}: {response.text}")

json = response.json()

Expand Down Expand Up @@ -1405,7 +1415,13 @@ async def make_rest_call(self, subpath: str, body: Dict[str, Any]) -> Dict:
timeout=20,
)

response.raise_for_status()
try:
response.raise_for_status()
except httpx.HTTPStatusError:
message = f"Failed to call {subpath}: {response.text}"
logger.error(message)
raise Exception(message)

json = response.json()

return json
Expand Down Expand Up @@ -2406,10 +2422,14 @@ async def add_generation_to_dataset(

# Prompt API

async def get_or_create_prompt_lineage(self, name: str, description: Optional[str] = None):
async def get_or_create_prompt_lineage(
self, name: str, description: Optional[str] = None
):
return await self.gql_helper(*create_prompt_lineage_helper(name, description))

get_or_create_prompt_lineage.__doc__ = LiteralAPI.get_or_create_prompt_lineage.__doc__
get_or_create_prompt_lineage.__doc__ = (
LiteralAPI.get_or_create_prompt_lineage.__doc__
)

@deprecated('Please use "get_or_create_prompt_lineage" instead.')
async def create_prompt_lineage(self, name: str, description: Optional[str] = None):
Expand Down Expand Up @@ -2476,4 +2496,3 @@ async def get_my_project_id(self):
return response["projectId"]

get_my_project_id.__doc__ = LiteralAPI.get_my_project_id.__doc__

2 changes: 1 addition & 1 deletion literalai/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.603"
__version__ = "0.0.604"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="literalai",
version="0.0.603", # update version in literalai/version.py
version="0.0.604", # update version in literalai/version.py
description="An SDK for observability in Python applications",
author="Literal AI",
package_data={"literalai": ["py.typed"]},
Expand Down
Loading