AI/ML API Documentation
API KeyModelsPlaygroundGitHubGet Support
  • 📞Contact Sales
  • 🗯️Send Feedback
  • Quickstart
    • 🧭Documentation Map
    • Setting Up
    • Supported SDKs
  • API REFERENCES
    • 📒All Model IDs
    • Text Models (LLM)
      • Alibaba Cloud
        • qwen-max
        • qwen-plus
        • qwen-turbo
        • Qwen2-72B-Instruct
        • Qwen2.5-7B-Instruct-Turbo
        • Qwen2.5-72B-Instruct-Turbo
        • Qwen2.5-Coder-32B-Instruct
        • Qwen-QwQ-32B
        • Qwen3-235B-A22B
      • Anthracite
        • magnum-v4
      • Anthropic
        • Claude 3 Haiku
        • Claude 3.5 Haiku
        • Claude 3 Opus
        • Claude 3 Sonnet
        • Claude 3.5 Sonnet
        • Claude 3.7 Sonnet
      • Cohere
        • command-r-plus
      • DeepSeek
        • DeepSeek V3
        • DeepSeek R1
        • DeepSeek Prover V2
      • Google
        • gemini-1.5-flash
        • gemini-1.5-pro
        • gemini-2.0-flash-exp
        • gemini-2.0-flash
        • gemini-2.5-flash-preview
        • gemini-2.5-pro-exp
        • gemini-2.5-pro-preview
        • gemma-2
        • gemma-3
      • Gryphe
        • MythoMax-L2-13b-Lite
      • Meta
        • Llama-3-chat-hf
        • Llama-3-8B-Instruct-Lite
        • Llama-3.1-8B-Instruct-Turbo
        • Llama-3.1-70B-Instruct-Turbo
        • Llama-3.1-405B-Instruct-Turbo
        • Llama-3.2-11B-Vision-Instruct-Turbo
        • Llama-3.2-90B-Vision-Instruct-Turbo
        • Llama-Vision-Free
        • Llama-3.2-3B-Instruct-Turbo
        • Llama-3.3-70B-Instruct-Turbo
        • Llama-4-scout
        • Llama-4-maverick
      • MiniMax
        • text-01
        • abab6.5s-chat
      • Mistral AI
        • codestral-2501
        • mistral-nemo
        • mistral-tiny
        • Mistral-7B-Instruct
        • Mixtral-8x22B-Instruct
        • Mixtral-8x7B-Instruct
      • NVIDIA
        • Llama-3.1-Nemotron-70B-Instruct-HF
        • llama-3.1-nemotron-70b
      • NeverSleep
        • llama-3.1-lumimaid
      • NousResearch
        • Nous-Hermes-2-Mixtral-8x7B-DPO
      • OpenAI
        • gpt-3.5-turbo
        • gpt-4
        • gpt-4-preview
        • gpt-4-turbo
        • gpt-4o
        • gpt-4o-mini
        • gpt-4o-audio-preview
        • gpt-4o-mini-audio-preview
        • gpt-4o-search-preview
        • gpt-4o-mini-search-preview
        • o1
        • o1-mini
        • o1-preview
        • o3-mini
        • gpt-4.5-preview
        • gpt-4.1
        • gpt-4.1-mini
        • gpt-4.1-nano
        • o4-mini
      • xAI
        • grok-beta
        • grok-3-beta
        • grok-3-mini-beta
    • Image Models
      • Flux
        • flux-pro
        • flux-pro/v1.1
        • flux-pro/v1.1-ultra
        • flux-realism
        • flux/dev
        • flux/dev/image-to-image
        • flux/schnell
      • Google
        • Imagen 3
        • Imagen 4 Preview
      • OpenAI
        • DALL·E 2
        • DALL·E 3
      • RecraftAI
        • Recraft v3
      • Stability AI
        • Stable Diffusion v3 Medium
        • Stable Diffusion v3.5 Large
    • Video Models
      • Alibaba Cloud
        • Wan 2.1 (Text-to-Video)
      • Google
        • Veo2 (Image-to-Video)
        • Veo2 (Text-to-Video)
      • Kling AI
        • v1-standard/image-to-video
        • v1-standard/text-to-video
        • v1-pro/image-to-video
        • v1-pro/text-to-video
        • v1.6-standard/text-to-video
        • v1.6-standard/image-to-video
        • v1.6-pro/image-to-video
        • v1.6-pro/text-to-video
        • v1.6-standard/effects
        • v1.6-pro/effects
        • v2-master/image-to-video
        • v2-master/text-to-video
      • Luma AI
        • Text-to-Video v2
        • Text-to-Video v1 (legacy)
      • MiniMax
        • video-01
        • video-01-live2d
      • Runway
        • gen3a_turbo
        • gen4_turbo
    • Music Models
      • MiniMax
        • minimax-music [legacy]
        • music-01
      • Stability AI
        • stable-audio
    • Voice/Speech Models
      • Speech-to-Text
        • stt [legacy]
        • Deepgram
          • nova-2
        • OpenAI
          • whisper-base
          • whisper-large
          • whisper-medium
          • whisper-small
          • whisper-tiny
      • Text-to-Speech
        • Deepgram
          • aura
    • Content Moderation Models
      • Meta
        • Llama-Guard-3-11B-Vision-Turbo
        • LlamaGuard-2-8b
        • Meta-Llama-Guard-3-8B
    • 3D-Generating Models
      • Stability AI
        • triposr
    • Vision Models
      • Image Analysis
      • OCR: Optical Character Recognition
        • Google
          • Google OCR
        • Mistral AI
          • mistral-ocr-latest
      • OFR: Optical Feature Recognition
    • Embedding Models
      • Anthropic
        • voyage-2
        • voyage-code-2
        • voyage-finance-2
        • voyage-large-2
        • voyage-large-2-instruct
        • voyage-law-2
        • voyage-multilingual-2
      • BAAI
        • bge-base-en
        • bge-large-en
      • Google
        • textembedding-gecko
        • text-multilingual-embedding-002
      • OpenAI
        • text-embedding-3-large
        • text-embedding-3-small
        • text-embedding-ada-002
      • Together AI
        • m2-bert-80M-retrieval
  • Solutions
    • Bagoodex
      • AI Search Engine
        • Find Links
        • Find Images
        • Find Videos
        • Find the Weather
        • Find a Local Map
        • Get a Knowledge Structure
    • OpenAI
      • Assistants
        • Assistant API
        • Thread API
        • Message API
        • Run and Run Step API
        • Events
  • Use Cases
    • Create Images: Illustrate an Article
    • Animate Images: A Children’s Encyclopedia
    • Create an Assistant to Discuss a Specific Document
    • Create a 3D Model from an Image
    • Create a Looped GIF for a Web Banner
    • Read Text Aloud and Describe Images: Support People with Visual Impairments
    • Find Relevant Answers: Semantic Search with Text Embeddings
    • Summarize Websites with AI-Powered Chrome Extension
  • Capabilities
    • Completion and Chat Completion
    • Streaming Mode
    • Code Generation
    • Thinking / Reasoning
    • Function Calling
    • Vision in Text Models (Image-To-Text)
    • Web Search
    • Features of Anthropic Models
    • Model comparison
  • FAQ
    • Can I use API in Python?
    • Can I use API in NodeJS?
    • What are the Pro Models?
    • How to use the Free Tier?
    • Are my requests cropped?
    • Can I call API in the asynchronous mode?
    • OpenAI SDK doesn't work?
  • Errors and Messages
    • General Info
    • Errors with status code 4xx
    • Errors with status code 5xx
  • Glossary
    • Concepts
  • Integrations
    • 🧩Our Integration List
    • Cline
    • Langflow
    • LiteLLM
    • Roo Code
Powered by GitBook
On this page
  • Example in Python
  • Synchronous call:
  • Asynchronous call:

Was this helpful?

  1. FAQ

Can I call API in the asynchronous mode?

Sure, any of our available models. Let's see how this works with an example in Python.

Example in Python

Below, we will see how two requests are handled when the second one is shorter and lighter than the first. We will compare synchronous processing (first example) and asynchronous processing (second example). After each example, the Response section shows the model's output for both queries. Pay attention to the order in which the answers are returned in each response!

Synchronous call:

from openai import OpenAI

def complete_chat(question):
    api_key = '<YOUR_AIMLAPI_KEY>'
    client = OpenAI(
        base_url='https://api.aimlapi.com',
        api_key=api_key,
    )    

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": question}],
    )
    print(f"Response for: {question}\n{response}\n")

def main():
    long_question = "List the 5 most famous hockey players of the 20th century."
    short_question = "What is 2+2?"

    # Execute both requests sequentially
    complete_chat(long_question)
    complete_chat(short_question)

if __name__ == "__main__":
    main()
Response
Response for: List the 5 most famous hockey players of the 20th century.
ChatCompletion(id='chatcmpl-B2cvJsSA2txXAYTWIfxIg55DYBchm', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The 20th century saw numerous iconic hockey players who made significant impacts 
on the game. Here are five of the most famous:\n\n1. **Wayne Gretzky** - Often referred to as "The Great One," Gretzky set numerous records and is widely considered the greatest hockey player of all time. He played the majority of his career 
in the NHL with the Edmonton Oilers and the Los Angeles Kings.\n\n2. **Bobby Orr** - Renowned for revolutionizing the role of the defenseman in hockey, Orr\'s incredible speed, scoring, and playmaking abilities made him a standout player, primarily with the Boston Bruins.\n\n3. **Gordie Howe** - Known as "Mr. Hockey," Howe\'s career spanned several decades, and he was famous for his toughness, skill, and scoring ability. He played the majority of his career with the Detroit Red Wings.\n\n4. **Mario Lemieux** - Known as "Super Mario," Lemieux was a dominant force with the Pittsburgh Penguins, overcoming numerous health challenges to become one of the game\'s all-time greats.\n\n5. **Maurice "Rocket" Richard** - As a prolific goal scorer, Richard became the first player to score 50 goals in a season and 500 in a career. He played his entire career with the Montreal Canadiens and was an inspiration to generations of players.\n\nEach of these players not only excelled on the ice but also left a lasting legacy on the sport.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1739965977, model='gpt-4o-2024-08-06', object='chat.completion', service_tier=None, system_fingerprint='fp_523b9b6e5f', usage=CompletionUsage(completion_tokens=9293, prompt_tokens=231, total_tokens=9524, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))

Response for: What is 2+2?
ChatCompletion(id='chatcmpl-B2cvP4PDesi5QipRvtzNHH6vYkszq', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='2+2 equals 4.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1739965983, model='gpt-4o-2024-08-06', object='chat.completion', service_tier=None, system_fingerprint='fp_523b9b6e5f', usage=CompletionUsage(completion_tokens=252, prompt_tokens=147, total_tokens=399, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))

Asynchronous call:

import asyncio
from openai import AsyncOpenAI

async def complete_chat(question):
    api_key = '<YOUR_API_KEY>'
    client = AsyncOpenAI(
        base_url='https://api.aimlapi.com',
        api_key=api_key,
    )    

    response = await client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": question}],
    )
    print(f"Response for: {question}\n{response}\n")

async def main():
    long_question = "List the 5 most famous hockey players of the 20th century."
    short_question = "What is 2+2?"

    # Run both requests concurrently
    await asyncio.gather(
        complete_chat(long_question),
        complete_chat(short_question),
    )

if __name__ == "__main__":
    try:
        asyncio.run(main())  # Works in a regular Python script
    except RuntimeError:
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())  # Works in Jupyter and other environments
Response
Response for: What is 2+2?
ChatCompletion(id='chatcmpl-B2cmWSVgRW5N7bq4Plj3C39881Fc3', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='2 + 2 equals 4.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1739965432, model='gpt-4o-2024-08-06', object='chat.completion', service_tier=None, system_fingerprint='fp_523b9b6e5f', usage=CompletionUsage(completion_tokens=284, prompt_tokens=147, total_tokens=431, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))

Response for: List the 5 most famous hockey players of the 20th century.
ChatCompletion(id='chatcmpl-B2cmWL39tvXjlmSGBuA1ckWDiOqQ5', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The 20th century saw many legendary hockey players who left a significant impact 
on the sport. Here are five of the most famous:\n\n1. **Wayne Gretzky** - Often referred to as "The Great One," Gretzky is widely considered the best hockey player of all time. His records and achievements have set the standard for excellence in the NHL.\n\n2. **Gordie Howe** - Known as "Mr. Hockey," Howe\'s career spanned five decades, and he was renowned for 
his scoring ability, physical play, and longevity in the sport.\n\n3. **Bobby Orr** - Orr revolutionized the defenseman position with his offensive skills and skating ability. He is most famous for his time with the Boston Bruins and his iconic "flying goal."\n\n4. **Maurice "Rocket" Richard** - A goal-scoring machine, Richard became the first player in NHL history to score 50 goals in a single season and 500 in a career. He was an icon for the Montreal Canadiens and a hero in Quebec.\n\n5. **Mario Lemieux** - Known as "Super Mario," Lemieux was an incredibly skilled player who overcame health challenges to become one of the most prolific scorers in NHL history.\n\nThese players not only dominated in their respective eras but also contributed to the evolution and popularity of hockey worldwide.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1739965432, model='gpt-4o-2024-08-06', object='chat.completion', service_tier=None, system_fingerprint='fp_523b9b6e5f', usage=CompletionUsage(completion_tokens=8537, prompt_tokens=231, total_tokens=8768, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))

As we can see, in the case of asynchronous execution, the response to a shorter or lighter query may be returned faster than the response to a longer or more complex one, even if the lighter query was formally queued second.

PreviousAre my requests cropped?NextOpenAI SDK doesn't work?

Last updated 2 months ago

Was this helpful?