# Error connecting to lm studio

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-connecting-to-lm-studio%2F139792%3E%5C
**Category:** Beginners\
**Created:** [February 9, 2025, 2:33am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-connecting-to-lm-studio%2F139792 "2025-02-09T02:33:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oasf2012](https://avatars.discourse-cdn.com/v4/letter/o/f19dbf/32.png) [@oasf2012](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Foasf2012)%5C
**Post date:** [February 9, 2025, 2:33am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-connecting-to-lm-studio%2F139792%2F1 "2025-02-09T02:33:41Z")

</div>

I have llm studio installed locally and I’m trying to use the openai to connect to it and I’m getting connection error , here is my code , what is the problem

import openai  
import re  
import getpass

openai.base\_url = “[http://127.0.0.1:1234](http://127.0.0.1:1234)”  
openai.api\_key = “”

def complete(prompt: str) → str:  
try:

```
    response = openai.chat.completions.create(
        model="deepseek-r1-distill-qwen-7b", # Ensure this matches your local model's name
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7, # Adjust as needed
        max_tokens=100 # Limit response length
    )
    return response.choices[0].message.content
except Exception as e:
    print(f"Error: {e}")
    return ""

```

def main():  
user\_input=“is it working”  
result = complete(user\_input)  
print(result)

if **name** == “ **main** ”:  
main()

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [February 9, 2025, 8:50am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-connecting-to-lm-studio%2F139792%2F2 "2025-02-09T08:50:03Z")

</div>

I don’t know much about LM Studio, but I found a case study of a similar error.

> <https://github.com/zou-group/textgrad/issues/78>
>
> Hi, I tried to use local LLM with lm-studio but it returned me as connection err…or. My sample code is modified from 
> 
> https://github.com/zou-group/textgrad/blob/main/textgrad/engine/local\_model\_openai\_api.py
> 
> First, it seems that the example code does not work, because prompt is not an argument, but content, for ChatExternalClient.
> 
> For the code:
> 
> \`\`\`python
> from openai import OpenAI
> from textgrad.engine.local\_model\_openai\_api import ChatExternalClient
> 
> client = OpenAI(base\_url="http://localhost:1234/v1", api\_key="lm-studio")
> engine = ChatExternalClient(client=client, model\_string="your-model-name")
> print(engine.generate(max\_tokens=40, prompt="What is the meaning of life?"))
> \`\`\`
> 
> I met such an error:
> 
> \`\`\`
> st\_to, options, remaining\_retries, stream, stream\_cls)
> 1011 log.debug("Raising connection error")
> \-\> 1012 raise APIConnectionError(request=request) from err
> 1014 log.debug(
> 1015 'HTTP Response: %s %s "%i %s" %s',
> 1016 request.method,
> (...)
> 1020 response.headers,
> 1021 )
> 
> APIConnectionError: Connection error.
> \`\`\`
> 
> Does this mean I cannot create a client from local machine? Thanks.

---

<div class="post-metadata">

**Author:** ![bayerth](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bayerth/32/40549_2.png) [@bayerth](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbayerth)%5C
**Post date:** [February 12, 2026, 9:12am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-connecting-to-lm-studio%2F139792%2F3 "2026-02-12T09:12:33Z")

</div>

Try it by using OpenAI client:

```auto
from openai import OpenAI

client = OpenAI(
        base_url="http://localhost:1234/v1",
        api_key="if necessary "
        )

message = [{"role": "user", "content": "Hello"}]
response = client.chat.completions.create(
        temperature=1,
        messages=message,
        model="qwen/qwen3-4b-2507",
        seed=42
        )
print(response.choices[0].message.content)

```
