mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-06 16:16:37 +08:00
Add final SSE error events for streaming endpoints when retries are exhausted
* Initial plan * Add final SSE error events for all streaming services Co-authored-by: bbbugg <80089841+bbbugg@users.noreply.github.com> * revert openai --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bbbugg <80089841+bbbugg@users.noreply.github.com> Co-authored-by: bbbugg <daming20120101@163.com> Enhance error handling by extracting nested JSON from error messages in SSE events Enhance error handling for content generation and streaming endpoints Enhance error handling for content generation and streaming endpoints Enhance error handling for content generation and streaming endpoints Enhance error handling for content generation and streaming endpoints 还原vertex和openai的更改,只保留gemini
This commit is contained in:
committed by
bbbugg
co-authored by
bbbugg
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
bbbugg
parent
4af17ce55d
commit
1771555fe9
@@ -470,6 +470,7 @@ class GeminiChatService:
|
||||
is_success = False
|
||||
status_code = None
|
||||
final_api_key = api_key
|
||||
last_error_msg = None
|
||||
|
||||
while retries < max_retries:
|
||||
request_datetime = datetime.datetime.now()
|
||||
@@ -509,6 +510,7 @@ class GeminiChatService:
|
||||
retries += 1
|
||||
is_success = False
|
||||
error_log_msg = str(e)
|
||||
last_error_msg = error_log_msg
|
||||
logger.warning(
|
||||
f"Streaming API call failed with error: {error_log_msg}. Attempt {retries} of {max_retries}"
|
||||
)
|
||||
@@ -553,3 +555,26 @@ class GeminiChatService:
|
||||
latency_ms=latency_ms,
|
||||
request_time=request_datetime,
|
||||
)
|
||||
|
||||
# Emit final error SSE event if all retries failed
|
||||
if not is_success:
|
||||
# 从错误消息中提取嵌套JSON
|
||||
parsed_error = None
|
||||
if last_error_msg:
|
||||
try:
|
||||
# 查找JSON起始位置
|
||||
json_start = last_error_msg.find('{')
|
||||
if json_start != -1:
|
||||
json_str = last_error_msg[json_start:]
|
||||
parsed_error = json.loads(json_str)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
error_data = {
|
||||
"error": {
|
||||
"code": parsed_error['error']['code'] if (parsed_error and 'error' in parsed_error and 'code' in parsed_error['error']) else (status_code or 500),
|
||||
"message": parsed_error['error']['message'] if (parsed_error and 'error' in parsed_error and 'message' in parsed_error['error']) else (last_error_msg or "Streaming failed"),
|
||||
"status": parsed_error['error']['status'] if (parsed_error and 'error' in parsed_error and 'status' in parsed_error['error']) else "INTERNAL"
|
||||
}
|
||||
}
|
||||
yield json.dumps(error_data, ensure_ascii=False)
|
||||
@@ -740,4 +740,4 @@ class OpenAIChatService:
|
||||
status_code=status_code,
|
||||
latency_ms=latency_ms,
|
||||
request_time=request_datetime,
|
||||
)
|
||||
)
|
||||
@@ -400,4 +400,4 @@ class GeminiChatService:
|
||||
status_code=status_code,
|
||||
latency_ms=latency_ms,
|
||||
request_time=request_datetime,
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user