Skip to content

Commit 924bee9

Browse files
🛠️ apply pre-commit fixes
1 parent 2db3a16 commit 924bee9

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

src/seer/automation/agent/client.py

+21-14
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,17 @@ def _get_config(cls, model_name: str):
103103
@staticmethod
104104
def is_completion_exception_retryable(exception: Exception) -> tuple[bool, bool]:
105105
# First check for context length errors that need trimming
106-
if isinstance(exception, openai.BadRequestError) and "maximum context length" in str(exception).lower():
106+
if (
107+
isinstance(exception, openai.BadRequestError)
108+
and "maximum context length" in str(exception).lower()
109+
):
107110
return True, True # Retry with message trimming
108-
111+
109112
# Original logic for other retryable errors
110113
is_retryable = isinstance(exception, openai.InternalServerError) or isinstance(
111114
exception, LlmStreamTimeoutError
112115
)
113-
116+
114117
return is_retryable, False # Regular retry without trimming
115118

116119
def generate_text(
@@ -460,9 +463,13 @@ def _get_config(cls, model_name: str):
460463
@staticmethod
461464
def is_completion_exception_retryable(exception: Exception) -> tuple[bool, bool]:
462465
# First check for context length errors that need trimming
463-
if isinstance(exception, anthropic.AnthropicError) and "413" in str(exception) and "Prompt is too long" in str(exception):
466+
if (
467+
isinstance(exception, anthropic.AnthropicError)
468+
and "413" in str(exception)
469+
and "Prompt is too long" in str(exception)
470+
):
464471
return True, True # Retry with message trimming
465-
472+
466473
# Original logic for other retryable errors
467474
retryable_errors = (
468475
"overloaded_error",
@@ -473,7 +480,7 @@ def is_completion_exception_retryable(exception: Exception) -> tuple[bool, bool]
473480
isinstance(exception, anthropic.AnthropicError)
474481
and any(error in str(exception) for error in retryable_errors)
475482
) or isinstance(exception, LlmStreamTimeoutError)
476-
483+
477484
return is_retryable, False # Regular retry without trimming
478485

479486
@observe(as_type="generation", name="Anthropic Generation")
@@ -1508,35 +1515,35 @@ def trim_messages_for_context_limit(messages, preserve_first=2, preserve_last=3)
15081515
"""
15091516
Trims messages from the middle of a list when they're too large for context windows.
15101517
Preserves the first and last few messages to maintain conversation coherence.
1511-
1518+
15121519
Args:
15131520
messages: List of Message objects to trim
15141521
preserve_first: Number of messages to preserve from the beginning
15151522
preserve_last: Number of messages to preserve from the end
1516-
1523+
15171524
Returns:
15181525
A new list with fewer messages, with middle messages summarized
15191526
"""
15201527
# Always preserve at least the first and last message
15211528
preserve_first = max(preserve_first, 1)
15221529
preserve_last = max(preserve_last, 1)
1523-
1530+
15241531
if len(messages) <= preserve_first + preserve_last:
15251532
return messages
1526-
1533+
15271534
trimmed_messages = []
15281535
trimmed_messages.extend(messages[:preserve_first])
1529-
1536+
15301537
# Add a summary message in the middle
15311538
middle_summary = Message(
15321539
role="system",
1533-
content=f"{len(messages) - preserve_first - preserve_last} messages were removed to reduce context length."
1540+
content=f"{len(messages) - preserve_first - preserve_last} messages were removed to reduce context length.",
15341541
)
15351542
trimmed_messages.append(middle_summary)
1536-
1543+
15371544
# Add the last few messages
15381545
trimmed_messages.extend(messages[-preserve_last:])
1539-
1546+
15401547
return trimmed_messages
15411548

15421549
def construct_message_from_stream(

src/seer/utils.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class MaxTriesExceeded(Exception):
8383
pass
8484

8585

86-
8786
def backoff_on_exception(
8887
is_exception_retryable,
8988
max_tries=4,
@@ -112,29 +111,29 @@ def wrapped_func(*args, **kwargs):
112111
result = func(*args, **kwargs)
113112
except Exception as exception:
114113
last_exception = exception
115-
114+
116115
# Check if the exception is retryable
117116
retry_info = is_exception_retryable(exception)
118-
117+
119118
# Handle both bool and tuple returns for backward compatibility
120119
if isinstance(retry_info, tuple):
121120
should_retry, needs_trimming = retry_info
122121
else:
123122
should_retry, needs_trimming = retry_info, False
124-
123+
125124
if should_retry:
126125
# If we need trimming and have messages, trim them and retry immediately
127126
if needs_trimming and "messages" in kwargs and kwargs["messages"]:
128127
from seer.automation.agent.client import trim_messages_for_context_limit
129-
128+
130129
original_messages = kwargs["messages"]
131130
kwargs["messages"] = trim_messages_for_context_limit(original_messages)
132-
131+
133132
logger.info(
134133
f"Context length exceeded. Trimming messages from {len(original_messages)} to {len(kwargs['messages'])}."
135134
)
136135
continue # Skip sleep and retry immediately with trimmed messages
137-
136+
138137
# Standard backoff for other retryable errors
139138
sleep_sec = sleep_sec_scaler(num_tries) + jitterer()
140139
logger.info(

0 commit comments

Comments
 (0)