Custom GPTs vs OpenAPI path parameters
Seems that the AI can't do idiomatic API calls for a RESTful interface after all - or their HTTP client has some bug.
TLDR: Don't use path parameters, at all, they don't work properly. Either do POST with params in the body or else use query parameters and declare them as required.
I spent a whole evening banging my head against a brick wall yesterday. The initial problem looked simple:
- I have an api for cooking recipes/ingredients, bog-standard RESTful interface.
- I have a custom GPT that is specialized on giving inspiration about what to cook.
- Connect them!
Easier said than done. Let me explain.
The initial API spec:
I basically had the following actions defined in my openapi.json
spec:
POST /search
that takes a query string as the body and returns a list of IDs and recipe namesGET /recipes/{id}
that takes the ID as the path param (yes, correctly specified in detail) and returns the informations about a recipe
only these two are the relevant ones to showcase the problem.
What GPT does
- I added the
openapi.json
file as an action to my custom GPT, and the editor says its all correct. Cool! - Now I try using it by asking a question about some food
- It uses the
POST /search
action just fine - It asks me which of the recipes I'd like
- It uses the
GET /recipes/{id}
action - ERROR.
- It uses the
GET /recipes/{id}
action - ERROR
- It uses the
GET /recipes/{id}
action - ERROR
- ...
and so on.
In the debugging output on the GPT test playground I could inspect the request/response debug information, and it looked good. It literally says that it is calling /recipes
with the parameter id
of some correct value. My server answered with an error though.
I used curl
to directly test the same request against the same server, and it worked flawlessly.
The actual problem
Now this is kind of nasty to spot from their debugging hints in the online editor.
GPT actually made the request to /recipes
(the /{id}
part is stripped out!) and adds the parameter as a query parameter instead!
And after hours of trying various syntaxes and looking through the openai forums, it seems that path parameters just don't work in general.
The Solution(s)
That means for me: I have two options to get the same functionality somehow:
- POST request with a body (this works just fine)
- GET request with query parameters (this is what GPT does already)
It felt just wrong to use POST for getting some data by ID, it also can create trouble if you want to add some caching layers in front of your API. Therefore: query parameters it is! The new action shall be GET /get_recipe_details
with ?id=something
. Smells like very old PHP somehow, but I am fine with it, this API is not for humans/developers but solely for the machine to begin with.
Some small code refactorings later, and I got it running, and now it works flawlessly.
What saved my day quite a bit here: Thanks to Rust and actix-web, moving around these definitions and having the compiler telling you every mistake or missing/wrong configuration is extremely liberating. Also codegen from actix-web to openapi specification at runtime just works, no need to fiddle with these complex JSON/YAML files.
- Number of words: 540
- Reading time: 3 minutes
- Posted: 3 months ago
Linked Categories
Click around and find out ↓
Technology
Stay ahead of the tech curve! Discover cutting-edge tools, trends, and insights tailored for solopreneurs and indie hackers driving innovation.
Indie Hacking
Dive into the world of solo entrepreneurship. Get insights and strategies for building successful, self-reliant tech businesses.
Backend Development
Uncover the power behind the scenes of web services and applications, focusing on creating robust, scalable backend systems that support frontend experiences.
Rust
A language empowering everyone to build reliable and efficient software. Futuristic swiss army knife and probably the most powerful mainstream technology today
Portfolio Links
See the stuff I made ↓
fresh bites bot
Custom GPT as an alternative frontend to my website https://fresh-bites.online to allow for interactive recipe search and tweaking.
API
Traditional backend services with an API. Gets data from somewhere, processes it to extract valuable information, returns it on demand.
Related Posts →
GPT4o Just Landed And Will Be Free For All!
3 months agoThe latest OpenAI ChatGPT model just got reveiled and it will be free for everyone - but more importantly: the GPT Store will be, too!
Rewrite it in Rust: Fun Weekend & Happy Wife
3 months agoHow I rewrote a pet project in Rust, shipped it within 2 days start-to-finish, and gained social credit along the way.
The joy of traditional SSR website development
4 months agoHow I got my sanity back after years of JavaScript madness. Building websites finally is fun again - plus hosting and maintenance is much better!
The Solopreneur and AI assistants
4 months agoMy thoughts about how solopreneurs and indie hackers can leverage AI tools to delegate work tasks without having employees or paying freelancers.
Digital Assets
4 months agoLearn how to categorize, build, and leverage digital products like websites, chat assistants, ebooks, and apps to create a sustainable lifestyle business.
Latest Posts →
GPT4o Just Landed And Will Be Free For All!
3 months agoThe latest OpenAI ChatGPT model just got reveiled and it will be free for everyone - but more importantly: the GPT Store will be, too!
Rewrite it in Rust: Fun Weekend & Happy Wife
3 months agoHow I rewrote a pet project in Rust, shipped it within 2 days start-to-finish, and gained social credit along the way.
The joy of traditional SSR website development
4 months agoHow I got my sanity back after years of JavaScript madness. Building websites finally is fun again - plus hosting and maintenance is much better!
The Solopreneur and AI assistants
4 months agoMy thoughts about how solopreneurs and indie hackers can leverage AI tools to delegate work tasks without having employees or paying freelancers.
Digital Assets
4 months agoLearn how to categorize, build, and leverage digital products like websites, chat assistants, ebooks, and apps to create a sustainable lifestyle business.