Skip to content

Commit

Permalink
fix(openai): exception throw with pydantic v1 (#2262)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirga authored Nov 5, 2024
1 parent 347edcb commit 506cbfe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,32 @@ def should_send_prompts():
def dont_throw(func):
"""
A decorator that wraps the passed in function and logs exceptions instead of throwing them.
@param func: The function to wrap
@return: The wrapper function
Works for both synchronous and asynchronous functions.
"""
# Obtain a logger specific to the function's module
logger = logging.getLogger(func.__module__)

def wrapper(*args, **kwargs):
async def async_wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except Exception as e:
_handle_exception(e, func, logger)

def sync_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
logger.debug(
"OpenLLMetry failed to trace in %s, error: %s",
func.__name__,
traceback.format_exc(),
)
if Config.exception_logger:
Config.exception_logger(e)

return wrapper
_handle_exception(e, func, logger)

def _handle_exception(e, func, logger):
logger.debug(
"OpenLLMetry failed to trace in %s, error: %s",
func.__name__,
traceback.format_exc(),
)
if Config.exception_logger:
Config.exception_logger(e)

return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper


@dont_throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def is_streaming_response(response):


def model_as_dict(model):
if isinstance(model, dict):
return model
if version("pydantic") < "2.0.0":
return model.dict()
if hasattr(model, "model_dump"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ async def _process_image_item(item, trace_id, span_id, message_index, content_in
return {"type": "image_url", "image_url": {"url": url}}


@dont_throw
async def _set_prompts(span, messages):
if not span.is_recording() or messages is None:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,32 @@ async def start_as_current_span_async(tracer, *args, **kwargs):
def dont_throw(func):
"""
A decorator that wraps the passed in function and logs exceptions instead of throwing them.
@param func: The function to wrap
@return: The wrapper function
Works for both synchronous and asynchronous functions.
"""
# Obtain a logger specific to the function's module
logger = logging.getLogger(func.__module__)

def wrapper(*args, **kwargs):
async def async_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
return await func(*args, **kwargs)
except Exception as e:
logger.debug(
"OpenLLMetry failed to trace in %s, error: %s",
func.__name__,
traceback.format_exc(),
)
if Config.exception_logger:
Config.exception_logger(e)
_handle_exception(e, func, logger)

return wrapper
def sync_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
_handle_exception(e, func, logger)

def _handle_exception(e, func, logger):
logger.debug(
"OpenLLMetry failed to trace in %s, error: %s",
func.__name__,
traceback.format_exc(),
)
if Config.exception_logger:
Config.exception_logger(e)

return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper


def run_async(method):
Expand Down

0 comments on commit 506cbfe

Please sign in to comment.