<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chainless Team</title>
    <description>The latest articles on DEV Community by Chainless Team (@chainless).</description>
    <link>https://dev.to/chainless</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4272694%2F4b152304-d1ac-442b-a4e6-01e4d45e9999.png</url>
      <title>DEV Community: Chainless Team</title>
      <link>https://dev.to/chainless</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chainless"/>
    <language>en</language>
    <item>
      <title>Point the OpenAI SDK at any OpenAI-compatible uncensored endpoint</title>
      <dc:creator>Chainless Team</dc:creator>
      <pubDate>Tue, 01 Dec 2026 14:42:25 +0000</pubDate>
      <link>https://dev.to/chainless/point-the-openai-sdk-at-any-openai-compatible-uncensored-endpoint-ij4</link>
      <guid>https://dev.to/chainless/point-the-openai-sdk-at-any-openai-compatible-uncensored-endpoint-ij4</guid>
      <description>&lt;p&gt;To use an OpenAI-compatible uncensored endpoint with the official OpenAI SDKs, set two values: the base URL (for example &lt;code&gt;https://chainless.one/v1&lt;/code&gt;) and the API key, either in code or through the &lt;code&gt;OPENAI_BASE_URL&lt;/code&gt; and &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; environment variables. Then call &lt;code&gt;chat.completions.create&lt;/code&gt;, not &lt;code&gt;responses.create&lt;/code&gt;, because most compatible providers implement Chat Completions only. Everything else, including streaming and tool calls, works the same way it does against OpenAI.&lt;/p&gt;

&lt;h2&gt;
  
  
  What "OpenAI-compatible" means in practice
&lt;/h2&gt;

&lt;p&gt;A provider is OpenAI-compatible when it accepts the same HTTP requests as OpenAI for a subset of endpoints and returns responses in the same shape. The subset varies. Almost every provider implements &lt;code&gt;POST /v1/chat/completions&lt;/code&gt; and &lt;code&gt;GET /v1/models&lt;/code&gt;. Fewer implement embeddings, images, audio or the newer Responses API.&lt;/p&gt;

&lt;p&gt;That subset is the first thing to check before you write code. For the worked example in this article, the &lt;a href="https://chainless.one/docs/api" rel="noopener noreferrer"&gt;API reference&lt;/a&gt; lists exactly two endpoints: &lt;code&gt;GET /v1/models&lt;/code&gt; and &lt;code&gt;POST /v1/chat/completions&lt;/code&gt;, with server-sent events when &lt;code&gt;"stream": true&lt;/code&gt; and tool calls on every model. There is no &lt;code&gt;/v1/responses&lt;/code&gt;, no embeddings and no images endpoint. Any SDK call outside that set will fail with an HTTP error, which is expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure the SDK
&lt;/h2&gt;

&lt;h3&gt;
  
  
  With environment variables
&lt;/h3&gt;

&lt;p&gt;Both official SDKs read the key and the base URL from the environment. The &lt;a href="https://github.com/openai/openai-python" rel="noopener noreferrer"&gt;Python SDK&lt;/a&gt; README notes the &lt;code&gt;OPENAI_BASE_URL&lt;/code&gt; variable as an alternative to passing &lt;code&gt;base_url&lt;/code&gt;, and the client source reads &lt;code&gt;OPENAI_BASE_URL&lt;/code&gt; when no base URL is given. The &lt;a href="https://github.com/openai/openai-node" rel="noopener noreferrer"&gt;Node SDK&lt;/a&gt; client source documents &lt;code&gt;baseURL&lt;/code&gt; as defaulting to &lt;code&gt;process.env['OPENAI_BASE_URL']&lt;/code&gt;, with &lt;code&gt;https://api.openai.com/v1&lt;/code&gt; as the fallback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OPENAI_BASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://chainless.one/v1"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$CHAINLESS_API_KEY"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the least invasive option for an existing app: no code changes, and switching back is a matter of unsetting two variables. The downside is that every OpenAI client in the process now talks to the new endpoint. If your app uses OpenAI for one task and another provider for something else, configure clients explicitly.&lt;/p&gt;

&lt;h3&gt;
  
  
  In code
&lt;/h3&gt;

&lt;p&gt;Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://chainless.one/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"CHAINLESS_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"openai"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;baseURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://chainless.one/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CHAINLESS_API_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the spelling: &lt;code&gt;base_url&lt;/code&gt; in Python, &lt;code&gt;baseURL&lt;/code&gt; in Node. The Node option is not &lt;code&gt;baseUrl&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  List the models the endpoint serves
&lt;/h2&gt;

&lt;p&gt;Model ids are provider-specific. Do not guess them; ask the endpoint. The list call is also the quickest way to confirm that the base URL and key work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Against the example endpoint, the response is a standard list object: &lt;code&gt;{"object": "list", "data": [...]}&lt;/code&gt;, where each item carries &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;object: "model"&lt;/code&gt; and &lt;code&gt;owned_by&lt;/code&gt;. Some providers add extra fields such as a display name or a context length. The SDKs keep unknown fields on the object, so you can read them, but typed code will not know about them.&lt;/p&gt;

&lt;p&gt;Use the ids exactly as returned. A model id with a typo is the single most common reason for a 404 after switching providers. The current ids and their context and output limits are on the &lt;a href="https://chainless.one/models" rel="noopener noreferrer"&gt;models page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Send a request and stream it
&lt;/h2&gt;

&lt;p&gt;A plain request looks the same as it does against OpenAI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"deepseek-v4.1-uncensored"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Explain what a race condition is in two sentences."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Streaming adds &lt;code&gt;stream=True&lt;/code&gt; and returns an iterator of chunks. Each chunk has a &lt;code&gt;choices&lt;/code&gt; list whose &lt;code&gt;delta&lt;/code&gt; carries the new piece of text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"glm-5.3-fast-uncensored"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Write a haiku about log files."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;stream_options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"include_usage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;if chunk.choices&lt;/code&gt; guard is not decoration. The &lt;a href="https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create" rel="noopener noreferrer"&gt;Chat Completions reference&lt;/a&gt; says that with &lt;code&gt;include_usage&lt;/code&gt; the stream ends with an extra chunk whose &lt;code&gt;choices&lt;/code&gt; array is empty and whose &lt;code&gt;usage&lt;/code&gt; field holds the totals. Code that does &lt;code&gt;chunk.choices[0]&lt;/code&gt; unconditionally will raise an &lt;code&gt;IndexError&lt;/code&gt; on that last chunk.&lt;/p&gt;

&lt;p&gt;The Node version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"glm-5.3-fast-uncensored"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Write a haiku about log files."&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What usually breaks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The SDK examples now use the Responses API
&lt;/h3&gt;

&lt;p&gt;Both official READMEs now present the Responses API as the primary interface and describe Chat Completions as the previous standard, supported indefinitely. If you copy the first example from the README, you get &lt;code&gt;client.responses.create(...)&lt;/code&gt;. A provider without &lt;code&gt;/v1/responses&lt;/code&gt; answers that with a 404, which looks like a bad URL but is really an unsupported endpoint. Use &lt;code&gt;client.chat.completions.create&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;The same trap exists one layer up. The &lt;a href="https://ai-sdk.dev/providers/ai-sdk-providers/openai" rel="noopener noreferrer"&gt;Vercel AI SDK OpenAI provider docs&lt;/a&gt; say the default &lt;code&gt;openai('model-id')&lt;/code&gt; factory uses the Responses API, and that you should create models with &lt;code&gt;openai.chat('model-id')&lt;/code&gt; when your custom base URL only supports Chat Completions. Other frameworks have similar switches. Check which endpoint your wrapper calls before blaming the provider.&lt;/p&gt;

&lt;h3&gt;
  
  
  The base URL is one level off
&lt;/h3&gt;

&lt;p&gt;The SDKs append paths such as &lt;code&gt;/chat/completions&lt;/code&gt; to the base URL. So the base URL must end at the version segment:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Base URL you set&lt;/th&gt;
&lt;th&gt;Request goes to&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://chainless.one/v1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/v1/chat/completions&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;works&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://chainless.one&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/chat/completions&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;404&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://chainless.one/v1/chat/completions&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/v1/chat/completions/chat/completions&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;404&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Parameters the provider ignores or rejects
&lt;/h3&gt;

&lt;p&gt;Compatible does not mean identical. Some providers ignore parameters they do not implement, others reject them with a 400. Typical candidates are &lt;code&gt;logprobs&lt;/code&gt;, &lt;code&gt;n&lt;/code&gt; greater than one, &lt;code&gt;seed&lt;/code&gt;, and OpenAI-specific fields like &lt;code&gt;service_tier&lt;/code&gt;. If a request that worked against OpenAI fails with a 400 after the switch, remove optional parameters one by one until it passes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retries hide the real error
&lt;/h3&gt;

&lt;p&gt;Both SDKs retry some failures by default: the READMEs list connection errors, 408, 409, 429 and 5xx, retried twice with a short backoff. That is reasonable in production, but while you are setting things up, it can turn a quick 429 into a long pause. Set &lt;code&gt;max_retries=0&lt;/code&gt; (Python) or &lt;code&gt;maxRetries: 0&lt;/code&gt; (Node) during debugging so errors surface immediately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Typed responses and extra fields
&lt;/h3&gt;

&lt;p&gt;Models that reason may return extra fields in the message or the streaming delta that the OpenAI types do not declare. The SDKs keep them on the object, but a strict type checker or a schema validator in your own code may complain. Read them defensively, and do not assume they exist on every provider.&lt;/p&gt;

&lt;p&gt;Once a single request and a streamed request both work, the rest of the app usually follows with no further changes. The API reference linked above has curl versions of each call, which are useful for checking whether a failure comes from the SDK setup or from the request itself.&lt;/p&gt;

</description>
      <category>python</category>
      <category>javascript</category>
      <category>llm</category>
      <category>ai</category>
    </item>
    <item>
      <title>How to uncensor an LLM with abliteration: the mechanics, step by step</title>
      <dc:creator>Chainless Team</dc:creator>
      <pubDate>Sun, 29 Nov 2026 20:38:41 +0000</pubDate>
      <link>https://dev.to/chainless/how-to-uncensor-an-llm-with-abliteration-the-mechanics-step-by-step-348j</link>
      <guid>https://dev.to/chainless/how-to-uncensor-an-llm-with-abliteration-the-mechanics-step-by-step-348j</guid>
      <description>&lt;p&gt;Abliteration uncensors an open-weight LLM without any training. You run the model on two prompt sets, one it refuses and one it answers, subtract the average internal activations of the two sets to get a "refusal direction", and then edit every weight matrix that writes into the residual stream so it can no longer write along that direction. The result is a normal checkpoint that has lost most of its ability to refuse, plus some side effects you should measure before you trust it.&lt;/p&gt;

&lt;p&gt;This article walks through the mechanics as the original research describes them, then covers the practical failure modes. It does not ship a ready-made script; the public tools linked below already do that, and their READMEs are the right place for exact commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  The finding the technique rests on
&lt;/h2&gt;

&lt;p&gt;The method comes from &lt;a href="https://arxiv.org/abs/2406.11717" rel="noopener noreferrer"&gt;Refusal in Language Models Is Mediated by a Single Direction&lt;/a&gt; by Arditi, Obeso, Syed, Paleka, Panickssery, Gurnee and Nanda (2024). Across 13 open chat models up to 72B parameters, the authors found one vector per model that controls refusal. Remove that vector's component from the model's hidden states and the model stops refusing harmful instructions. Add it to the hidden states on a harmless prompt and the model refuses something it would normally answer.&lt;/p&gt;

&lt;p&gt;The paper is careful about what this means. It calls the work an existence proof rather than an optimised recipe, and it notes that the direction might encode something closer to "this looks harmful" than "refuse". For a practitioner the useful part is simpler: a refusal decision passes through a low-dimensional bottleneck, and a bottleneck can be cut.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: two prompt sets and a cache of activations
&lt;/h2&gt;

&lt;p&gt;You need instructions the model reliably refuses and instructions it reliably answers. The paper drew the first group from public harmful-instruction sets and the second from Alpaca, with 128 training and 32 validation examples per class. The widely read &lt;a href="https://huggingface.co/blog/mlabonne/abliteration" rel="noopener noreferrer"&gt;Hugging Face write-up by Maxime Labonne&lt;/a&gt; uses similar sources repackaged as small Hub datasets.&lt;/p&gt;

&lt;p&gt;Both sets go through the model with the chat template applied. At every layer you record the residual stream, the running vector that each attention block and MLP block reads from and adds to. The paper records it at the token positions after the instruction, where the model is about to start its reply. The Labonne write-up takes the last token only, which costs less compute and is usually close enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: difference in means gives candidate directions
&lt;/h2&gt;

&lt;p&gt;For each layer and position, average the cached activations of the refused set, average those of the answered set, and subtract. The difference, normalised to unit length, is one candidate refusal direction. With dozens of layers you now have dozens of candidates, and most of them are poor.&lt;/p&gt;

&lt;p&gt;The paper picks one with three checks on held-out prompts:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;What it measures&lt;/th&gt;
&lt;th&gt;What a good candidate does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bypass score&lt;/td&gt;
&lt;td&gt;refusals on harmful prompts with the direction ablated&lt;/td&gt;
&lt;td&gt;drops sharply&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Induce score&lt;/td&gt;
&lt;td&gt;refusals on harmless prompts with the direction added&lt;/td&gt;
&lt;td&gt;rises above zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;KL score&lt;/td&gt;
&lt;td&gt;how much the output distribution on harmless prompts moves under ablation&lt;/td&gt;
&lt;td&gt;stays small&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It also skips candidates from the last fifth of the layers, because a direction too close to the output head tends to change word choice rather than the decision. The refusal measure itself is fast to compute: instead of generating text, the authors compare the probability the model gives to its usual refusal-opening tokens against everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: directional ablation and weight orthogonalization
&lt;/h2&gt;

&lt;p&gt;There are two ways to apply the chosen direction &lt;code&gt;r&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inference-time ablation.&lt;/strong&gt; On every forward pass, at every layer and token, subtract the projection of the hidden state onto &lt;code&gt;r&lt;/code&gt;. This needs hooks in the inference code and is handy for experiments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Weight orthogonalization.&lt;/strong&gt; Every matrix that writes into the residual stream is changed so its output has no component along &lt;code&gt;r&lt;/code&gt;. In Labonne's implementation that is the token embedding matrix, each attention block's output projection and each MLP's output projection; the paper also includes output biases. The paper shows this edit is mathematically equivalent to inference-time ablation, and the result is an ordinary set of weights you can save, quantize and serve anywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;orthogonalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;W&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Tensor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Tensor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Tensor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""W writes into the residual stream (d_model x d_in); r is the refusal direction (d_model)."""&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;norm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;W&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; @ &lt;span class="no"&gt;W&lt;/span&gt;   &lt;span class="c1"&gt;# remove the component of every output along r&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That one line, applied to the right matrices, is the whole edit. Everything else in the pipeline is about choosing &lt;code&gt;r&lt;/code&gt; well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools that automate the search
&lt;/h2&gt;

&lt;p&gt;Doing this by hand means a lot of layer-by-layer trial. Three public projects show the common approaches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FailSpy's &lt;a href="https://github.com/FailSpy/abliterator" rel="noopener noreferrer"&gt;abliterator&lt;/a&gt; is a small MIT-licensed library on top of TransformerLens. It caches activations, computes and tests candidate directions, applies them to chosen layers and helps check for degradation. The Labonne article credits FailSpy's notebook as its base.&lt;/li&gt;
&lt;li&gt;The Labonne write-up itself is a full notebook using TransformerLens, and it picks the layer by reading generations from the top candidates.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/p-e-w/heretic" rel="noopener noreferrer"&gt;Heretic&lt;/a&gt; wraps the edit in an optimiser. It uses Optuna's TPE sampler to search ablation parameters and scores each trial by refusal rate and KL divergence from the original model, so it looks for the smallest edit that still removes refusals. The repository is archived and read-only at the time of writing, so check its README for the version and licence (AGPL-3.0) before you build on it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What goes wrong
&lt;/h2&gt;

&lt;p&gt;Abliteration is a blunt cut through a model that was not designed to have that part removed. The common problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Quality loss.&lt;/strong&gt; The paper found MMLU, ARC and GSM8K scores mostly stayed within confidence intervals, with exceptions for two models, but TruthfulQA dropped consistently. Labonne's 8B example lost ground on every benchmark he ran. He recovered most of it with a DPO pass afterwards, though not on GSM8K. Run your own task suite before and after.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Residual refusals.&lt;/strong&gt; One direction from a few hundred prompts does not cover every phrasing. Expect occasional refusals, half-answers, or a reply wrapped in disclaimers, especially in topic areas your prompt sets did not include.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrong layer.&lt;/strong&gt; A direction from a layer too late can make the model ramble or switch tone. Too early, and the edit may barely change refusals. The KL check exists to catch the first case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bad contrast sets.&lt;/strong&gt; If the refused and answered sets differ in length, format or language, the mean difference captures that too, and you ablate something you did not mean to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quantization after the edit.&lt;/strong&gt; A low-bit quant of an abliterated checkpoint compounds two kinds of damage. Test the exact file you will run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing is added.&lt;/strong&gt; The edit removes a behaviour. It does not add knowledge, and a model that no longer refuses will confidently answer questions it cannot answer. Treat facts, names and citations in its output as claims to verify.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Doing it yourself or using a hosted model
&lt;/h2&gt;

&lt;p&gt;Abliterating a small model is a reasonable weekend project if you have the GPU memory to run it in full precision with activation caching. For frontier-size open models the weights alone are hundreds of gigabytes, and the evaluation loop multiplies that work. If what you need is the behaviour rather than the experiment, hosted uncensored models are an option: Chainless serves several, listed on &lt;a href="https://chainless.one/models" rel="noopener noreferrer"&gt;the models page&lt;/a&gt;, behind one OpenAI-compatible endpoint documented at &lt;a href="https://chainless.one/docs/api" rel="noopener noreferrer"&gt;chainless.one/docs/api&lt;/a&gt;. Either way, run the same before-and-after checks described above on prompts from your own work.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>ai</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
