From 0bfc70be63521ed02e2bdd3393d945cbb9b86a46 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Fri, 5 Jul 2024 12:40:02 +0200 Subject: [PATCH 1/4] fix(sdk): AGE-272 Propagate func errors up in @ag.instrument() wrappers Func errors used to be set as func result. Now they are propagated up by forwarding the exception up (raise e). --- .../agenta/sdk/decorators/llm_entrypoint.py | 2 ++ agenta-cli/agenta/sdk/decorators/tracing.py | 33 ++++++++++--------- agenta-cli/pyproject.toml | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/agenta-cli/agenta/sdk/decorators/llm_entrypoint.py b/agenta-cli/agenta/sdk/decorators/llm_entrypoint.py index e18ae1bc4..4ee869222 100644 --- a/agenta-cli/agenta/sdk/decorators/llm_entrypoint.py +++ b/agenta-cli/agenta/sdk/decorators/llm_entrypoint.py @@ -83,9 +83,11 @@ async def wrapper(*args, **kwargs) -> Any: {"config": config_params, "environment": "playground"} ) + # Exceptions are all handled inside self.execute_function() llm_result = await self.execute_function( func, *args, params=func_params, config_params=config_params ) + return llm_result @functools.wraps(func) diff --git a/agenta-cli/agenta/sdk/decorators/tracing.py b/agenta-cli/agenta/sdk/decorators/tracing.py index a15874049..b7571477c 100644 --- a/agenta-cli/agenta/sdk/decorators/tracing.py +++ b/agenta-cli/agenta/sdk/decorators/tracing.py @@ -58,19 +58,20 @@ async def async_wrapper(*args, **kwargs): try: result = await func(*args, **kwargs) self.tracing.update_span_status(span=span, value="OK") - except Exception as e: - result = str(e) - self.tracing.set_span_attribute( - {"traceback_exception": traceback.format_exc()} - ) - self.tracing.update_span_status(span=span, value="ERROR") - finally: self.tracing.end_span( outputs=( {"message": result} if not isinstance(result, dict) else result ) ) - return result + return result + + except Exception as e: + self.tracing.set_span_attribute( + {"traceback_exception": traceback.format_exc()} + ) + self.tracing.update_span_status(span=span, value="ERROR") + self.tracing.end_span(outputs={}) + raise e @wraps(func) def sync_wrapper(*args, **kwargs): @@ -89,17 +90,19 @@ def sync_wrapper(*args, **kwargs): try: result = func(*args, **kwargs) self.tracing.update_span_status(span=span, value="OK") - except Exception as e: - result = str(e) - self.tracing.set_span_attribute( - {"traceback_exception": traceback.format_exc()} - ) - self.tracing.update_span_status(span=span, value="ERROR") - finally: self.tracing.end_span( outputs=( {"message": result} if not isinstance(result, dict) else result ) ) + return result + + except Exception as e: + self.tracing.set_span_attribute( + {"traceback_exception": traceback.format_exc()} + ) + self.tracing.update_span_status(span=span, value="ERROR") + self.tracing.end_span(outputs={}) + raise e return async_wrapper if is_coroutine_function else sync_wrapper diff --git a/agenta-cli/pyproject.toml b/agenta-cli/pyproject.toml index 3ba8848ef..966176c9c 100644 --- a/agenta-cli/pyproject.toml +++ b/agenta-cli/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "agenta" -version = "0.17.5" +version = "0.17.6-alpha.1" description = "The SDK for agenta is an open-source LLMOps platform." readme = "README.md" authors = ["Mahmoud Mabrouk "] From 32a24862189d2e1ce633402b9adf74ceac5b8010 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Fri, 5 Jul 2024 13:05:27 +0200 Subject: [PATCH 2/4] fix(sdk): Add func error and stacktrace to result --- agenta-cli/agenta/sdk/decorators/tracing.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/agenta-cli/agenta/sdk/decorators/tracing.py b/agenta-cli/agenta/sdk/decorators/tracing.py index b7571477c..a99e91558 100644 --- a/agenta-cli/agenta/sdk/decorators/tracing.py +++ b/agenta-cli/agenta/sdk/decorators/tracing.py @@ -66,11 +66,15 @@ async def async_wrapper(*args, **kwargs): return result except Exception as e: + result = { + "message": str(e), + "stacktrace": traceback.format_exc(), + } self.tracing.set_span_attribute( {"traceback_exception": traceback.format_exc()} ) self.tracing.update_span_status(span=span, value="ERROR") - self.tracing.end_span(outputs={}) + self.tracing.end_span(outputs=result) raise e @wraps(func) @@ -98,11 +102,15 @@ def sync_wrapper(*args, **kwargs): return result except Exception as e: + result = { + "message": str(e), + "stacktrace": traceback.format_exc(), + } self.tracing.set_span_attribute( {"traceback_exception": traceback.format_exc()} ) self.tracing.update_span_status(span=span, value="ERROR") - self.tracing.end_span(outputs={}) + self.tracing.end_span(outputs=result) raise e return async_wrapper if is_coroutine_function else sync_wrapper From 3c359427a8a1091eb2ce101258e5a1396f02f810 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Fri, 5 Jul 2024 17:47:15 +0200 Subject: [PATCH 3/4] Update pyproject.toml --- agenta-cli/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenta-cli/pyproject.toml b/agenta-cli/pyproject.toml index 966176c9c..80a898978 100644 --- a/agenta-cli/pyproject.toml +++ b/agenta-cli/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "agenta" -version = "0.17.6-alpha.1" +version = "0.17.6a1" description = "The SDK for agenta is an open-source LLMOps platform." readme = "README.md" authors = ["Mahmoud Mabrouk "] From 2d2ee83141759c5f5caf43b9541f9b67a82998a3 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Fri, 5 Jul 2024 18:36:33 +0200 Subject: [PATCH 4/4] Update pyproject.toml --- agenta-cli/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenta-cli/pyproject.toml b/agenta-cli/pyproject.toml index 80a898978..3ba8848ef 100644 --- a/agenta-cli/pyproject.toml +++ b/agenta-cli/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "agenta" -version = "0.17.6a1" +version = "0.17.5" description = "The SDK for agenta is an open-source LLMOps platform." readme = "README.md" authors = ["Mahmoud Mabrouk "]