---
id: 18183
lang: en
title: "GigaChat API breaks OpenAI-style code in six places: what to change in Node.js"
published_at: "2026-11-25 12:27:01"
source_name: "Habr AI"
source_url: "https://habr.com/ru/articles/1112158/?utm_campaign=1112158&utm_source=habrahabr&utm_medium=rss"
canonical_url: "https://news.hamidun.com/en/news/18183"
categories: ["llm"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

# GigaChat API breaks OpenAI-style code in six places: what to change in Node.js

> **Краткая суть (BLUF):** It seems that switching from OpenAI to GigaChat only requires changing the base URL and the key, but in practice the code breaks in six places. The access token lives just 30 minutes, the servers are signed by a root certificate from the Ministry of Digital Development, images are uploaded through a storage service, and function arguments arrive as an object rather than a string. The author connected GigaChat to a Node.js service and collected the errors along with workarounds. As a result, a connection that should take "a day" easily turns into a week if you don't know the differences in advance.  - The token is valid for 30 minutes, and the RqUID header must be a new uuid4 for every exchange - The Ministry of Digital Development root certificate is best passed only to the GigaChat client, without disabling verification - An image is first uploaded to storage, then its id is placed in attachments

A Node.js service already talks to several OpenAI-compatible providers, and it seems that a new one can be connected simply by changing the base URL and the key. With GigaChat, that doesn't work: the author of the review on Habr counted six differences, and each one breaks code written for OpenAI. For teams choosing a Russian model or required to use it by a customer's requirements, this is a ready-made checklist. It saves days of debugging through logs.

## Authorization and the certificate: two obstacles before the first response

The personal account issues an "authorization key" that already encodes the Client ID and secret. It does not go into requests to the model. First it is exchanged for an access token through a separate request to a different host, and the token lives for 30 minutes. There are two traps here. The RqUID header is mandatory and must be a new uuid4 for every exchange, otherwise you get a 400. The scope parameter must match the project's plan: GIGACHAT_API_PERS, GIGACHAT_API_B2B or GIGACHAT_API_CORP.

The token can be refreshed in advance based on expires_at, or reactively when the API returns a 401. The author chose the second option: it survives a process restart and does not depend on a server with inaccurate clocks. All requests go through a single wrapper that, on a 401, obtains a new token and retries the call once.

The second problem is the certificate. The servers are signed by the root authority of the Ministry of Digital Development (Russian Trusted Root CA), and it is not in the standard trust lists. The very first request fails with a CERTIFICATE_VERIFY_FAILED error. The most tempting route is to disable verification, but then the connection stops confirming that the other end is really GigaChat. Installing the certificate into the system is not a way out either: Node uses its own built-in set of root certificates. Where the system store is read, the Ministry's root would start vouching for any site for all processes on the machine. The author keeps the certificate next to the code and passes it only to a separate undici client used for GigaChat. He advises taking the file from the certificates page on Gosuslugi: it is a trust anchor, and its provenance matters.

## Files, response schemas and functions

In OpenAI, an image is attached to a message as image_url. In GigaChat, it is first uploaded to storage, you receive an id, and you put that id into an attachments array. According to the documentation: one image per message, up to 10 per request, up to 15 MB per file. JPEG, PNG, TIFF and BMP are declared as supported, while storage rejects WebP with the error File format is not supported. So the author converts everything except JPEG and PNG before uploading. He determines the format from the file's first bytes, because Telegram, for example, delivers files as application/octet-stream. Uploaded files remain in the account until they are deleted, so after the model's response you need to call deletion, and cleanup errors are better not propagated.

JSON-schema responses are also structured differently. In OpenAI the schema sits at response_format.json_schema.schema, while in GigaChat it sits directly at response_format.schema. A request in OpenAI format returns the error Empty schema with json format type is not supported. A small adapter is enough. Without required and strict, the model may return arbitrary JSON, so the result should still be validated on your side. In /v2/chat/completions this field has moved to model_options.response_format.

The most unpleasant differences are in function calling in /v1. Instead of tools you use functions, instead of tool_calls you get function_call, and the result is returned with the role function. And arguments arrive as an object rather than a JSON string, so the usual JSON.parse fails. A type check before parsing is enough. But an agent loop written for OpenAI won't work without an adapter. That's why the author doesn't use GigaChat in agents with tools yet. For single requests (classification, field extraction, answers from a knowledge base) it works without problems.

## Images through chat and the practical takeaway

There is no separate endpoint for image generation. The chat draws the picture: the request goes out with "function_call": "auto" and a request to draw. The response contains not a file but an HTML img tag with an id inside the text. You need to extract the id with a regular expression and download the file with a separate request to /files/{id}/content, otherwise the user will see the tag instead of the picture.

What follows in practice: each difference takes about an hour to resolve, but together they turn "we'll connect it in a day" into a week-long task if you learn about them from errors in logs. It is more sensible to build a separate adapter layer from the start: a wrapper with token refresh, a dedicated HTTP client with the root certificate, format conversion, and bringing schemas and function calls to a common shape. Then the rest of the service keeps working with a single OpenAI-like interface.

There are limitations too. The article describes the /v1 version and one author's experience on one project. Documentation and API behavior may change; for example, some fields have already moved to /v2. Before deployment, check the current limits for your plan and file formats. The author invites readers to share other API quirks in the comments.

---
- **Первоисточник:** [Habr AI](https://habr.com/ru/articles/1112158/?utm_campaign=1112158&utm_source=habrahabr&utm_medium=rss)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/18183
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
