Skip to content

Commit d45d5fd

Browse files
committed
Use correct error for functions
1 parent ab0c71d commit d45d5fd

File tree

9 files changed

+31
-34
lines changed

9 files changed

+31
-34
lines changed

agent_apis/src/functions/llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from dotenv import load_dotenv
55
from openai import OpenAI
6-
from restack_ai.function import FunctionFailure, function, log
6+
from restack_ai.function import NonRetryableError, function, log
77

88
load_dotenv()
99

@@ -47,4 +47,4 @@ async def llm(function_input: FunctionInputParams) -> str:
4747
return response.choices[0].message.content
4848
except Exception as e:
4949
error_message = "llm function failed"
50-
raise FunctionFailure(error_message, non_retryable=True) from e
50+
raise NonRetryableError(error_message) from e

agent_chat/src/functions/llm_chat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dotenv import load_dotenv
55
from openai import OpenAI
66
from pydantic import BaseModel
7-
from restack_ai.function import FunctionFailure, function, log
7+
from restack_ai.function import NonRetryableError, function, log
88

99
load_dotenv()
1010

@@ -22,7 +22,7 @@ class LlmChatInput(BaseModel):
2222

2323
def raise_exception(message: str) -> None:
2424
log.error(message)
25-
raise FunctionFailure(message, non_retryable=True)
25+
raise NonRetryableError(message)
2626

2727

2828
@function.defn()
@@ -48,8 +48,8 @@ async def llm_chat(agent_input: LlmChatInput) -> dict[str, str]:
4848
messages=agent_input.messages,
4949
)
5050
except Exception as e:
51-
log.error("llm_chat function failed", error=e)
52-
raise
51+
error_message = f"LLM chat failed: {e}"
52+
raise NonRetryableError(error_message) from e
5353
else:
5454
log.info(
5555
"llm_chat function completed", assistant_raw_response=assistant_raw_response

agent_rag/src/functions/llm_chat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from openai import OpenAI
66
from openai.types.chat.chat_completion import ChatCompletion
77
from pydantic import BaseModel
8-
from restack_ai.function import FunctionFailure, function, log
8+
from restack_ai.function import NonRetryableError, function, log
99

1010
load_dotenv()
1111

@@ -23,7 +23,7 @@ class LlmChatInput(BaseModel):
2323

2424
def raise_exception(message: str) -> None:
2525
log.error(message)
26-
raise FunctionFailure(message, non_retryable=True)
26+
raise NonRetryableError(message)
2727

2828

2929
@function.defn()
@@ -49,8 +49,8 @@ async def llm_chat(function_input: LlmChatInput) -> ChatCompletion:
4949
messages=function_input.messages,
5050
)
5151
except Exception as e:
52-
log.error("llm_chat function failed", error=e)
53-
raise
52+
error_message = f"LLM chat failed: {e}"
53+
raise NonRetryableError(error_message) from e
5454
else:
5555
log.info("llm_chat function completed", response=response)
5656
return response

agent_telephony/twilio/agent_twilio/src/functions/livekit_call.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from livekit import api
44
from livekit.protocol.sip import CreateSIPParticipantRequest, SIPParticipantInfo
5-
from restack_ai.function import FunctionFailure, function, log
5+
from restack_ai.function import NonRetryableError, function, log
66

77

88
@dataclass
@@ -37,8 +37,7 @@ async def livekit_call(function_input: LivekitCallInput) -> SIPParticipantInfo:
3737

3838
log.info("livekit_call SIPParticipantInfo:", participant=participant)
3939
except Exception as e:
40-
log.error("livekit_call function failed", error=str(e))
41-
failure_message = "livekit_call function failed: " + str(e)
42-
raise FunctionFailure(failure_message, non_retryable=True) from e
40+
error_message = f"livekit_call function failed: {e}"
41+
raise NonRetryableError(error_message) from e
4342
else:
4443
return participant

agent_telephony/twilio/agent_twilio/src/functions/livekit_outbound_trunk.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
ListSIPOutboundTrunkRequest,
77
SIPOutboundTrunkInfo,
88
)
9-
from restack_ai.function import FunctionFailure, function, function_info, log
9+
from restack_ai.function import NonRetryableError, function, function_info, log
1010

1111

1212
@function.defn()
@@ -45,9 +45,8 @@ async def livekit_outbound_trunk() -> str:
4545
await livekit_api.aclose()
4646

4747
except Exception as e: # Consider catching a more specific exception if possible
48-
log.error("livekit_outbound_trunk function failed", error=str(e))
49-
failure_message = "livekit_outbound_trunk function failed: " + str(e)
50-
raise FunctionFailure(failure_message, non_retryable=True) from e
48+
error_message = f"livekit_outbound_trunk function failed: {e}"
49+
raise NonRetryableError(error_message) from e
5150

5251
else:
5352
return trunk.sip_trunk_id

agent_telephony/vapi/agent_vapi/src/functions/vapi_call.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from dataclasses import dataclass
33

4-
from restack_ai.function import FunctionFailure, function, log
4+
from restack_ai.function import NonRetryableError, function, log
55
from vapi import AsyncVapi, Call
66

77

@@ -26,8 +26,7 @@ async def vapi_call(function_input: VapiCallInput) -> Call:
2626
log.info("vapi_call: ", call=call)
2727

2828
except Exception as e:
29-
log.error("vapi_call function failed", error=str(e))
30-
failure_message = "vapi_call function failed: " + str(e)
31-
raise FunctionFailure(failure_message, non_retryable=True) from e
29+
error_message = f"vapi_call function failed: {e}"
30+
raise NonRetryableError(error_message) from e
3231
else:
3332
return call

agent_todo/src/functions/llm_chat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010
from openai.types.chat.chat_completion_tool_param import ChatCompletionToolParam
1111
from pydantic import BaseModel
12-
from restack_ai.function import FunctionFailure, function, log
12+
from restack_ai.function import NonRetryableError, function, log
1313

1414
load_dotenv()
1515

@@ -30,7 +30,7 @@ class LlmChatInput(BaseModel):
3030

3131
def raise_exception(message: str) -> None:
3232
log.error("llm_chat function failed", error=message)
33-
raise FunctionFailure(message, non_retryable=True)
33+
raise NonRetryableError(message)
3434

3535

3636
@function.defn()
@@ -58,8 +58,8 @@ async def llm_chat(function_input: LlmChatInput) -> ChatCompletion:
5858
tools=function_input.tools,
5959
)
6060
except Exception as e:
61-
log.error("llm_chat function failed", error=e)
62-
raise
61+
error_message = f"LLM chat failed: {e}"
62+
raise NonRetryableError(error_message) from e
6363
else:
6464
log.info("llm_chat function completed", response=response)
6565
return response

agent_tool/src/functions/llm_chat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010
from openai.types.chat.chat_completion_tool_param import ChatCompletionToolParam
1111
from pydantic import BaseModel
12-
from restack_ai.function import FunctionFailure, function, log
12+
from restack_ai.function import NonRetryableError, function, log
1313

1414
load_dotenv()
1515

@@ -30,7 +30,7 @@ class LlmChatInput(BaseModel):
3030

3131
def raise_exception(message: str) -> None:
3232
log.error(message)
33-
raise FunctionFailure(message, non_retryable=True)
33+
raise NonRetryableError(message)
3434

3535

3636
@function.defn()
@@ -58,5 +58,5 @@ async def llm_chat(function_input: LlmChatInput) -> ChatCompletion:
5858
tools=function_input.tools,
5959
)
6060
except Exception as e:
61-
log.error("llm_chat function failed", error=e)
62-
raise
61+
error_message = f"LLM chat failed: {e}"
62+
raise NonRetryableError(error_message) from e

pdf_ocr/src/functions/torch_ocr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from numpy.typing import NDArray
88
from PIL import Image
99
from pydantic import BaseModel, Field
10-
from restack_ai.function import function, log, FunctionFailure
10+
from restack_ai.function import function, log, NonRetryableError
1111

1212
from doctr.models import ocr_predictor
1313
import requests
@@ -42,14 +42,14 @@ async def torch_ocr(input: OcrInput) -> str:
4242
processed_img: NDArray[np.uint8] = service._preprocess_image(image)
4343
doc = DocumentFile.from_images(processed_img)
4444
else:
45-
raise FunctionFailure("Unsupported file type", non_retryable=True)
45+
raise NonRetryableError("Unsupported file type")
4646

4747
result = service.predictor(doc)
4848
json_output = OCRPrediction.model_validate(result.export())
4949
return service._process_predictions(json_output)
5050
except Exception as e:
51-
log.error(f"Failed to process file: {str(e)}")
52-
raise FunctionFailure(f"Failed to process file: {str(e)}", non_retryable=True)
51+
error_message = f"Failed to process file: {str(e)}"
52+
raise NonRetryableError(error_message) from e
5353

5454
class DocumentExtractionService:
5555
def __init__(self) -> None:

0 commit comments

Comments
 (0)