ChatGPT Debug Assistant

I tried to implement a Flask Restful Endpoint with the webargs framework, which i already did back in 2020. Although, i copied the old code and made some tiny adjustments with the request call, i could not make it work in 2023. By asking ChatGPT for help, i gained a clearer understanding on all the components of the Framework. Eventually, it gave me good pointers on what to google. It turns out, that now the @use_kwargs decorator (starting with version 6.0.0) does need an additional parameter called location, to access parameters passed through the query. Although ChatGPT provided some example code, it did not include that hint, presumably, because it is information which changes through time. Nonetheless, it helped immensely to talk with a “rubber duck”, which has some additional background knowledge.

Creating a Flask Restful Endpoint with Webargs in 2020

In 2020 i developed a simple API endpoint for a home project and apparently used webargs==5.5. A snippet of the API and how it is called is described in the following snippet. Back then it would work just fine for me.

# Flask Restful Endpoint
class UsageTime(Resource):

	@use_kwargs( {"deviceID":fields.String(required=True) } )
	def get(self, deviceID):
		usageTime = readFile(deviceID,"usageTime")
		message = readFile(deviceID,"message")
		return {'usageTime':usageTime, 'message':message}, 200

# Powershell Get request
$r = Invoke-WebRequest -Uri ${server}:5000/UsageTime?deviceID=ID -TimeoutSec 60 -Method GET

Creating a Flask Restful Endpoint with Webargs in 2023

So for another project i just did the same

# Flask Restful Endpoint
class TopicOverview(Resource):

    @use_kwargs({"hash_id": fields.Str(required=True), })
    def get(self, hash_id):
        return {'message': 'Hello, ' + hash_id}, 200
# curl get request

However i would always raise an error such as

LookupError: no exception for None

# or if i remove the required=True
TypeError: get() missing 1 required positional argument: 'hash_id'

The error indicated, that somehow the GET argument couldn’t get parsed or couldn’t get passed correctly to my endpoint, but i did not know how to google this problem.

How ChatGPT Helped Me Debug

So i asked ChatGPT for help. The initial start was a bit rough, because i did not know, what my assistant would need from me and how to phrase my problem. Eventually i came upon a quite interesting answer:

To be honest, the explanation was not too helpful, but the provided code worked and my brain was now asking, why did it work. Apparently, the use_kwargs decorator does not recognize the parameters i passed in the query, but the passing of the parameter did work correctly. The next step would be to ask google, on why use_kwargs does not work as expected.

Which led to to a very similiar github issues with the webargs framework starting with version 6.0.0 (released just 2 months after my first attempt in 2020). With the new major update, it is apparently now required, to let @use_kwargs know where to extract the parameter from by setting the location='query' parameter.

On a further note, i also asked ChatGPT to refactor the above code to work with the webargs framework, however, it does not include the location keyword. My guess is, that either the training data did not include this information, or it can not handle changes of facts over time well enough just yet.

How to create a Python Flask RESTful Endpoint with Webargs

To conclude this blog post, here is the final code snippet for the endpoint of a GET request, which requires the parameter hash_id:

class TopicOverview(Resource):

    @use_kwargs({"hash_id": fields.Str(), }, location="query")
    def get(self, hash_id):
        return {'message': 'Hello, ' + hash_id}, 200

For some it might have been clear as day, but for me who never does frontend development, i already struggled through API requests and what to expect in a payload. ChatGPT helped, by explaining many concepts also troubleshooting on what components are more likely to cause errors.

Leave a Comment

Your email address will not be published. Required fields are marked *

hungsblog | Nguyen Hung Manh | Dresden
Scroll to Top