How to build a chat with the Responses API
Written by
Updated at October 28, 2025
Getting started
Python
Get API authentication credentials as described in Authentication with the Yandex Cloud AI Studio API.
Build a chat
Python
-
Create a file named
index.pyand add the following code to it:import openai YANDEX_CLOUD_MODEL = "yandexgpt-lite" client = openai.OpenAI( api_key=YANDEX_CLOUD_API_KEY, base_url="https://rest-assistant.api.cloud.yandex.net/v1", project=YANDEX_CLOUD_FOLDER ) previous_id = None # Saving the last response ID print("💬 Chat with GPT (enter 'exit' to exit)\n") while True: user_input = input("You: ") if user_input.lower() in ("exit", "quit"): print("Chat session ended.") break response = client.responses.create( model=f"gpt://{YANDEX_CLOUD_FOLDER}/{YANDEX_CLOUD_MODEL}", input=[{"role": "user", "content": user_input}], previous_response_id=previous_id # Providing context, if any ) # Saving the ID for the next step previous_id = response.id # Printing the agent's response print("Agent:", response.output_text) -
Save authentication data to environment variables:
export YANDEX_CLOUD_FOLDER=<folder_ID> export YANDEX_CLOUD_API_KEY=<API_key> -
Run the file you created:
python index.pyResponse example:
💬 Chat with GPT (enter 'exit' to exit) You: Hi! Agent: Hi there! How can I help you? You: What exactly can you do? Agent: I can answer questions, help solve various problems, and provide information on various topics. For example, I can tell you about the weather, help with translating a text, suggest ideas, or just keep the conversation going. What are you interested in? You: Tell me a joke Agent: Why do programmers always have a cup of coffee? Because when they are working on a hard problem, they need to overclock their central processing unit... and the coffee helps them do it! You: Exit Chat session ended.
See also
- Common instance models
- Examples of working with ML SDK on GitHub