Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

drf-spectacular skips @api_view and APIView endpoints in the schema

Your error

Error [ping]: unable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Either way you may want to add a serializer_class (or method). Ignoring view for now.1 more line it can show

Fix it fast

Most common cause, 1 of 3

An @api_view function has no @extend_schema

Add @extend_schema above @api_view, naming the request and response serializers.

Fix
from drf_spectacular.utils import extend_schema

@extend_schema(request=PingRequestSerializer, responses=PingSerializer)
@api_view(["POST"])
def ping(request):
    ...
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

drf-spectacular error unable to guess serializer means the schema generator met an @api_view function or plain APIView with no serializer_class, so it left that endpoint out. Put @extend_schema(request=..., responses=...) above @api_view, give the APIView a serializer_class or annotate its get and post methods, and use OpenApiViewExtension for library views. Applies to drf-spectacular 0.30.0.

  • Applies to drf-spectacular 0.30.0, Django REST framework 3.18.3, Django 6.1.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Generate the schema from the command line and list the views it names. The name in square brackets says which view to fix. A file path with no line number before Error usually means an @api_view function (cause 1); a path with a line number points at a class (cause 2 or 3).

Run this first
python manage.py spectacular --file schema.yml 2>&1 | grep "unable to guess serializer" | cut -c1-160

Then find the line you see in this list and open its fix:

  1. 1An @api_view function has no @extend_schemaThe bracket holds a function name, such as Error [ping], and the path has no line number
  2. 2A class based on APIView defines no serializer_class or get_serializer_classThe bracket holds a class name, such as Error [Health], with a line number in views.py
  3. 3The view comes from a library you cannot editThe path in front of Error points into site-packages, not into your project

What does the error look like?

  • Error [ping]: unable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Either way you may want to add a serializer_class (or method). Ignoring view for now.
  • views.py:13: Error [Health]: unable to guess serializer. This is graceful fallback handling for APIViews.

Cause 1 of 3

An @api_view function has no @extend_schema

You seeThe bracket holds a function name, such as Error [ping], and the path has no line number

Check

drf-spectacular reports unable to guess serializer for an @api_view function because the decorator turns the function into an APIView class that carries no serializer at all. With nothing to inspect, the generator logs the error and drops the endpoint from the schema.

Check
grep -n -B2 "@api_view" */views.py | grep -c extend_schema

Fix

Add @extend_schema above @api_view, naming the request and response serializers. For a function that serves several methods, wrap it in @extend_schema_view and describe each method separately.

Fix
from drf_spectacular.utils import extend_schema

@extend_schema(request=PingRequestSerializer, responses=PingSerializer)
@api_view(["POST"])
def ping(request):
    ...

Verify

The endpoint now appears under paths in schema.yml, and the run ends with Errors: 0.

Verify
python manage.py spectacular --file schema.yml --fail-on-warn && grep -n "/ping/" schema.yml

Confirmed by drf-spectacular.readthedocs.io

Did this fix it?

Cause 2 of 3

A class based on APIView defines no serializer_class or get_serializer_class

You seeThe bracket holds a class name, such as Error [Health], with a line number in views.py

Check

For a plain APIView, drf-spectacular first tries get_serializer, then get_serializer_class, then a serializer_class attribute. When none of them exist it emits this message. Putting @extend_schema on the class itself does not help, because the annotation must sit on the get, post or other entry method.

Check
grep -n "class .*(APIView)" -A3 */views.py | grep -c serializer_class

Fix

Give the view a serializer_class, or move it to GenericAPIView, or decorate each entry method with @extend_schema when the request and response use different serializers.

Fix
class Health(APIView):
    serializer_class = HealthSerializer

    def get(self, request):
        ...

# or, per method
class Health(APIView):
    @extend_schema(responses=HealthSerializer)
    def get(self, request):
        ...

Verify

The class name no longer appears in the output, and the endpoint has a response schema.

Verify
python manage.py spectacular --file schema.yml --fail-on-warn

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The view comes from a library you cannot edit

You seeThe path in front of Error points into site-packages, not into your project

Check

Third-party packages often ship @api_view or APIView endpoints without serializers, so their views trigger the same message and you cannot add a decorator to their code. drf-spectacular's extensions let you swap in a patched view only while the schema is built.

Check
python manage.py spectacular --file schema.yml 2>&1 | grep "site-packages" | grep -c "unable to guess"

Fix

Write an OpenApiViewExtension whose target_class is the library view, return a subclass that adds serializer_class, and import the module once, for example from your main app's urls.py.

Fix
# yourapp/schema.py
from drf_spectacular.extensions import OpenApiViewExtension

class LibraryViewFix(OpenApiViewExtension):
    target_class = "somelib.views.SomeView"

    def view_replacement(self):
        from yourapp.serializers import SomeSerializer

        class Fixed(self.target_class):
            serializer_class = SomeSerializer

        return Fixed

Verify

After importing yourapp.schema, the library endpoint is documented and its error line is gone.

Verify
python manage.py spectacular --file schema.yml --fail-on-warn

Confirmed by drf-spectacular.readthedocs.io

Did this fix it?

How do I stop it coming back?

  • Run manage.py spectacular --fail-on-warn in CI so a new unannotated endpoint fails the build instead of quietly vanishing from the docs.
  • Write new endpoints as GenericAPIView or ViewSet classes with serializer_class set, which drf-spectacular can read without hints.
  • Keep all extensions in one schema.py module and import it from a file that always loads.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.