FYI Using openai npm under Obsidian.md

Posting this here in case it helps someone else.

Well I just spent a stupid amount of time on this, so I will share in case it is relevant to someone else:

Symptoms: openai default client for Node.js (npm install -D openai) was correctly generating images via createImage but createImageEdit was failing with an error saying I didn’t attach the required property image only when running under Obsidian.

I spent a very long time thinking I was setting up the arguments wrong in some subtle way, when I finally decided to just trace through:

  • When running in regular Node.js, Axios detects no browser and uses the http adapter.
  • Under Obsidian, it detects XMLHttpRequest and uses the xhr adapter, which fails to serialize the attached streams into the request body. So it send a mostly empty POST request. I didn’t bother tracing exactly why it fails, but even the reference examples don’t work. With the http adapter, everything is fine.

To work around, I now do this:

class HackedOpenAI extends OpenAIApi {
    constructor(configuration: Configuration) {
        super(configuration);
        this.axios.defaults.adapter = require("axios/lib/adapters/http");
    }
}

Very very expensive 6 lines :slight_smile:

2 Likes

…and…

This is the first time derammo has posted — let’s welcome them to our community!

Bless you! Welcome to the forums. Hope you stick around!

1 Like