Creating a simple text agent with a function call
Written by
Updated at October 28, 2025
In Yandex AI Studio, you can create a text agent with a function call that can communicate with the user in text format and maintain a dialogue closely resembling natural human interaction.
Getting started
To use an example:
Python
Get API authentication credentials as described in Authentication with the Yandex AI Studio API.
Build a chat
Python
-
Create a file named
index.pyand add the following code to it:import openai from openai import OpenAI import json YANDEX_CLOUD_MODEL = "yandexgpt" client = openai.OpenAI( api_key=YANDEX_CLOUD_API_KEY, base_url="https://rest-assistant.api.cloud.yandex.net/v1", project=YANDEX_CLOUD_FOLDER ) # 1. Listing the functions the model can call tools = [ { "type": "function", "name": "get_weather", "description": "Getting current weather for the city you specify.", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "City, e.g.: Petersburg or Moscow", }, }, "required": ["city"], }, }, ] # The simplest implementation of the function (can be replaced with a call to a real API) def get_weather(city): # Here you can implement an integration, e.g., with Yandex Weather return { "city": city, "temperature": "12 °C", "condition": "Cloudy, light breeze" } # Forming a list of messages to be updated input_list = [ {"role": "user", "content": "What is the weather like in Krasnoyarsk?"} ] # 2. Requesting a model with specific features response = client.responses.create( model=f"gpt://{YANDEX_CLOUD_FOLDER}/{YANDEX_CLOUD_MODEL}", tools=tools, input=input_list, ) # Adding the model's input to history input_list += response.output for item in response.output: if item.type == "function_call": if item.name == "get_weather": # 3. Executing the get_weather function weather_info = get_weather(**json.loads(item.arguments)) # 4. Providing the result of the function back to the model input_list.append({ "type": "function_call_output", "call_id": item.call_id, "output": json.dumps(weather_info) }) print("Final input:") for item in input_list: if isinstance(item, dict) and item.get("type") == "function_call_output": parsed = json.loads(item["output"]) print("function_call_output:", parsed) else: print(item) response = client.responses.create( model=f"gpt://{YANDEX_CLOUD_FOLDER}/{YANDEX_CLOUD_MODEL}", instructions="Respond only with the weather data returned by the function.", tools=tools, input=input_list, ) # 5. Final response print("Final output:") print(response.model_dump_json(indent=2)) print("\n" + response.output_text) -
Save authentication data into environment variables:
export YANDEX_CLOUD_FOLDER=<folder_ID> export YANDEX_CLOUD_API_KEY=<API_key> -
Run the file you created:
python index.pyResponse example:
Final input: {'role': 'user', 'content': 'What is the weather like in Krasnoyarsk?'} ResponseFunctionToolCall(arguments='{"city":"Krasnoyarsk"}', call_id='get_weather', name='get_weather', type='function_call', id='get_weather', status='completed', valid=True) function_call_output: {'city': 'Krasnoyarsk', 'temperature': '12 °C', 'condition': 'Cloudy, light breeze'} Final output: { "id": "70d96fac-1c4b-4f4a-9f80-56df********", "created_at": 1758556157206.0, "error": null, "incomplete_details": null, "instructions": "Respond only with the weather data returned by the function.", "metadata": null, "model": "gpt://b1gstllj8rgs********/yandexgpt", "object": "response", "output": [ { "id": "f15c66e8-99a2-4647-a820-406e********", "content": [ { "annotations": [], "text": "It is currently plus 12°C in Krasnoyarsk, cloudy, light breeze.", "type": "output_text", "logprobs": null, "valid": true } ], "role": "assistant", "status": "completed", "type": "message", "valid": true } ], "parallel_tool_calls": true, "temperature": null, "tool_choice": "auto", "tools": [ { "name": "get_weather", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "City, e.g.: Petersburg or Moscow" } }, "required": [ "city" ] }, "strict": null, "type": "function", "description": "Getting current weather for the city you specify.", "valid": true } ], "top_p": null, "background": false, "conversation": null, "max_output_tokens": null, "max_tool_calls": null, "previous_response_id": null, "prompt": null, "prompt_cache_key": null, "reasoning": null, "safety_identifier": null, "service_tier": null, "status": "completed", "text": null, "top_logprobs": null, "truncation": null, "usage": null, "user": "", "valid": true } It is currently plus 12°C in Krasnoyarsk, cloudy, light breeze.