From 509120bf619cad438c216fefc35333bfa2c1e3d0 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Tue, 29 Aug 2023 16:28:07 -0700 Subject: [PATCH] add context window exceeded error for anthropic --- litellm/__pycache__/utils.cpython-311.pyc | Bin 74724 -> 75240 bytes litellm/llms/anthropic.py | 7 +++++-- litellm/tests/test_exceptions.py | 8 ++++---- litellm/utils.py | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/litellm/__pycache__/utils.cpython-311.pyc b/litellm/__pycache__/utils.cpython-311.pyc index 27a856762ee4837b5d81738f489fc5238276d58d..5ef3ac7b17bd4e50483a24eca7df7d7401d7c5e6 100644 GIT binary patch delta 2640 zcmbW2ZA@EL7{|}Kw=bimMOtX-3-tC4)>WWVUR|8%5))&HE+Ip|s2hiDFxoEVZsNSG zb4H^k8sdp=*`^_WU>cD`uSreR_-RhT$z>~9WkVO{eBUhUGNXL(+`f$4-gY57={;@l zJ?Hm4&;LBnbALFk_<2^LKdIAc02z}9uOZ)-5A-k2P{*|R=u8kki@%NRs+I9+DVEK$dwsU7b+(f%5(pT7ZyZ~(Na&O&pCi}_>$ms>kBv^vbG!p08~Nt z=<&*_y4a9DcN^WYCv7%rOB-J=?)4TDHbFL=j{1@gzT4mHc5wXzOg1X{w?K$_|!%)nY`=&r0RQZgwO zGSIrw>#T~#!fY)Tz4(sCszjTv&G#jm9~8{uCmHMzY644jok>r7!m}mmYDoH8@s97S z@E6}1pc@}v{0Mg9jtlkp;$jWoe&Mpp+}qaM1rlAFJ`KG<-%s@^>GMiupN0=Ct083g z&dWRnef;lNo}%HsJo=@bf{*gYe`}`T9z1*XGq?}GbnO%zcdCzA+;H}P DXhlci}A+pgAj8fx7pOr6^w{(*pxI(2q*v!W>7QblB<;=8+qUV5nudvM|9 z?)&+?&*yXB_wMExKl2msIA*t75F4il{w8I6WX;U4|k8NFBl@Lg^gx*Es*|=9hYeNEJ8O8{mV3#(;Lvdemju5fZ*^$OKcG-)! zh;6r8D%KO6;NpSeeO79hV3)J_|8w*uCXSQNQgS8?bX-g5_Md%815QE=DGYN=7Q3{u z0J*e8MYvd1sft0apO6}fXK~ag|DO>?v5~mcME4T6N>yuM@<_ZIgsV&t0;yF^&>w$s zCkutlxy$IBcy;u8uTQ66R>;U5kLvO%i=n;aO-Y}!$^cL5v|J2k>J9JLZ?r`MD%!82 zt-5s$Xy(a5|r1X66nnu2pHT+JJQ}{uweAV|LE+2lIB~yksAy~gGQtC zTs-SKwUY%ENT2TSb~I}78?tuN!|YjoFYx%8Cqjh7^#7+ zoQnQg7HG2;D2-)X?JbgtHC+>VRx*(mx+fb!`ruaLNgKD_)xc(4kg572ZllIHz+ z+{+2kDXS-4+kC1mW9^!-cID9qI(;Q!tBIs-kxTZPIV58G_3s^HbMM-Yu~$UFg>?OHVbI#T#%B&*gZbr&^B9m;r3^i`Qig9yx#G zJq}vsowqwNyexk;+m2x?9slPnyh#t|M}Ww$&t(C2%1w70F}y>&@B8Jq?%e{|FL&Rs z!sde}ga&pXnt2?N5B}Q+a7g~+K?lI6vh(5h7>>wekIiQICY2Sy#zAjtx(d!=$fS;1 p;fMg&Qk_fTC trigger an exception from an llm provider -> assert if output is of the expected type @@ -35,8 +34,8 @@ litellm.failure_callback = ["sentry"] # Approach: Run each model through the test -> assert if the correct error (always the same one) is triggered # models = ["gpt-3.5-turbo", "chatgpt-test", "claude-instant-1", "command-nightly"] -test_model = "gpt-3.5-turbo" -models = ["gpt-3.5-turbo"] +test_model = "claude-instant-1" +models = ["claude-instant-1"] def logging_fn(model_call_dict): @@ -50,7 +49,7 @@ def logging_fn(model_call_dict): # Test 1: Context Window Errors @pytest.mark.parametrize("model", models) def test_context_window(model): - sample_text = "how does a court case get to the Supreme Court?" * 1000000 + sample_text = "how does a court case get to the Supreme Court?" * 50000 messages = [{"content": sample_text, "role": "user"}] try: print(f"model: {model}") @@ -64,6 +63,7 @@ def test_context_window(model): return except InvalidRequestError as e: print(f"InvalidRequestError: {e.llm_provider}") + print(f"InvalidRequestError message: {e.message}") return except OpenAIError as e: print(f"OpenAIError: {e.llm_provider}") diff --git a/litellm/utils.py b/litellm/utils.py index a43da00c8..f802e7f69 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -1357,6 +1357,14 @@ def exception_type(model, original_exception, custom_llm_provider): else: exception_type = "" if "claude" in model: # one of the anthropics + if hasattr(original_exception, "message"): + if "prompt is too long" in original_exception.message: + exception_mapping_worked = True + raise ContextWindowExceededError( + message=original_exception.message, + model=model, + llm_provider="anthropic" + ) if hasattr(original_exception, "status_code"): print_verbose(f"status_code: {original_exception.status_code}") if original_exception.status_code == 401: @@ -1372,6 +1380,12 @@ def exception_type(model, original_exception, custom_llm_provider): model=model, llm_provider="anthropic", ) + elif original_exception.status_code == 413: + exception_mapping_worked = True + raise ContextWindowExceededError( + message=f"AnthropicException - {original_exception.message}", + llm_provider="anthropic", + ) elif original_exception.status_code == 429: exception_mapping_worked = True raise RateLimitError(