API Doc-CometAPI
English
  • English
  • Русский
HomeDashBoardModel Marketplace
HomeDashBoardModel Marketplace
Discord_Support
English
  • English
  • Русский
  1. 🏄🏼‍♀️ Best Practices
  • How to Use CometAPI?
  • 🚀 Overview
    • Quick Start
    • Important Guidelines
    • Release Notes
    • Quickly request CometAPI via ApiDog
    • Models
  • 💬 Text Models
    • Chat
    • Responses
    • Anthropic Messages
    • Gemini Generating Content
    • Embeddings
  • 🖼️ Image Models
    • OpenAI
      • gpt-4o-image generates image
      • Images
      • Image Editing (gpt-image-1)
    • Midjourney
      • Midjourney Quick Start: Complete Image Generation Workflow in One Go
      • Task Fetching API
        • List by Condition
        • Fetch Single Task (most recommended)
      • Submit Editor
      • Imagine
      • Submit Video
      • Action (UPSCALE; VARIATION; REROLL; ZOOM, etc.)
      • Blend (image -> image)
      • Describe (image -> text)
      • Modal (Area Redesign & Zoom)
    • Replicate(image)
      • Create Predictions - General
      • replicate query
    • seededit/seedream
      • bytedance-Image Editing (seededit)
      • bytedance-image-generation(seedream)
    • bria
      • Generate Image
      • Generate Vector Graphics - Base (Beta)
      • Image Editing
      • Query Status
    • Gemini
      • Guide to calling gemini-2.5-flash-image (Nano Banana)
      • Gemini generates image
    • Hunyuan3D
  • 📺 Video Models
    • sora-2
      • official
        • Create video
        • Remix video
        • Retrieve video
        • Retrieve video content
      • self-developed
        • Retrieve video
        • Create video
    • veo3
      • veo3-chat format
      • Submit video generation task
      • Query video generation status
    • kling (video)
      • callback_url
      • Multimodal Video Editing
        • Initialize Video for Editing
        • Add Video Selection
        • Delete Video Selection
        • Clear Video Selection
        • Preview Selected Video Area
        • Create Task
      • Text to Video
      • Image to Video
      • Multi-Image To Video
      • Video Extension
      • Avatar
      • Lip-Sync
      • Video Effects
      • Text to Audio
      • Video to Audio
      • TTS
      • Image Generation
      • Multi-Image to Image
      • Image Expansion
      • Image Recognize
      • Virtual Try-On
      • [Counterpart] Creating Tasks
      • lip sync
      • Individual queries
    • runway(video)
      • official format
        • runway images raw video
        • Generate a video from a video
        • Generate an image from text
        • Upscale a video
        • Control a character
        • runway to get task details
      • Reverse Format
        • generate(text)
        • generate(Reference images)
        • Video to Video Style Redraw
        • Act-one Expression Migration
        • feed-get task
    • bytedance
      • bytedance-video
      • bytedance-video get
    • MiniMax Conch(video)
      • MiniMax Conch Official Documentation
      • MiniMax Conch Generation
      • MiniMax Conch Query
      • MiniMax Conch Download
  • 🎵 Music Models
    • Suno
      • Setting suno Version
      • Suno API Scenario Application Guide
      • Generate lyrics
      • Generate music clip
      • Upload clip
      • Submit concatenation
      • Full Track Audio Separation
      • Single Track Audio Separation
      • Create New Persona
      • add style tags
      • Single task query
      • Generate mp4 mv video
      • Timing: lyrics, audio timeline
      • Get wav format file
      • Get midi
      • Batch query tasks
  • 🔊 Audio Models
    • Realtime
    • Create speech
    • Create transcription
    • Create translation
  • 🧩 Integration Guides
    • LiteLLM
    • Dify
    • Make
    • n8n
    • Lobe-Chat
    • COZE
    • Zapier
    • Activepieces
    • LlamaIndex
    • Continue
    • FlowiseAI
    • Chatbox
    • CherryStudio
    • AnythingLLM
    • LangChain
    • BuildShip
    • gptme
    • Immersive Translation
    • Cline
    • Eudic Translation
    • ChatHub
    • OpenAI Translator
    • ChatAll Translation
    • Pot Translation
    • Zotero
    • NEXT CHAT (ChatGPT Next Web)
    • Obsidian's Text Generator Plugin
    • librechat
    • utools-ChatGPT Friend
    • avante.nvim
    • Open WebUI
    • GPT Academic Optimization (gpt_academic)
    • OpenManus
    • IntelliJ Translation Plugin
    • FastGPT
    • n8n Local Deployment
  • ⚠️ Errors
    • Error Codes & Handling
  • 📝 Code Examples
    • Text-to-Image Generation
    • Image-to-image generation URL upload
    • Regular Post Text Conversation
    • OpenAI Official Library Usage Example
    • Streamed Output
    • Json Fixed Format Output Code Display
    • Embedding code example
    • o1-preview Model Code Example
    • LangChain Usage Example (Successful Test Date: 2024-11-25)
    • Openai dall-e-3 & flux series drawing model
    • gpt, claude, gemini multimodal network image parsing example
    • Multimodal PDF File Parsing Examples for GPT, Claude, and Gemini
    • Code example
  • 🏄🏼‍♀️ Best Practices
    • Midjourney Best Practices
    • Retry Logic Documentation for CometAPI and OpenAI Official API
    • Runway Best Practices
    • Claude Code Installation and Usage Guide
    • Gemini CLI Installation and Usage Guide
    • Codex Usage Guide
    • CometAPI Account Balance Query API Usage Instructions
  • 💳 Pricing & Billing
    • About Pricing
  • 🤝🏼 Support
    • Help Center
    • Interface Stability
    • Privacy policy
    • Terms of service
    • Common Misconceptions
    • Confusion about use
  1. 🏄🏼‍♀️ Best Practices

Retry Logic Documentation for CometAPI and OpenAI Official API

Introduction#

This documentation aims to help you understand how to implement automatic request switching and retry logic when using cometapi and OpenAI official API. Priority is given to initiating requests through cometapi, and when error responses are received, the system automatically switches to OpenAI official API to retry the request.

Retry Logic Overview#

The retry logic is divided into two main steps:
1.
Prioritize cometapi: First attempt to make API requests using cometapi. If the request is successful, return the response result. If the request fails (e.g., encounters an HTTP error), proceed to step two.
2.
Switch to OpenAI Official API: When a cometapi request fails, the program automatically switches to OpenAI official API to make the same request. If the request is successful, return the response result. If the request fails, return an error message to the user.

Implementation Details#

Environment Setup#

Before starting implementation, ensure you have installed the necessary Python libraries. This example uses OpenAI's Python client library, so you need to make sure it's installed:

Implementation Steps#

Here are the specific implementation steps:
1.
Initialize API Clients:
Create a cometapi client instance, set its base_url and api_key.
Create an OpenAI official API client instance, only requiring setting the api_key.
2.
Implement Request Logic:
Attempt to make requests through the cometapi client.
Catch potential API errors (such as 400 or 500 errors). These errors are typically caused by request failures or error codes in responses.
If an error occurs, switch to the OpenAI official API client and make the same request.
For each request method, record and output error information (if any).
3.
Return Response or Error Message:
If any request method succeeds, return the request result message.
If both request methods fail, return a string containing error information.

Example Code#

Below is a Python example code implementing the retry logic:

Notes#

Error Handling: In production environments, extend error logging functionality for better error handling and recording.
Performance Monitoring: Frequent switching to OpenAI official API may indicate issues with cometapi that require further investigation and optimization.
The implementation of this retry logic is designed to improve system robustness and request success rate, ensuring that the system can still provide normal service when specific API nodes experience problems.
Modified at 2025-05-16 07:32:17
Previous
Midjourney Best Practices
Next
Runway Best Practices
Built with