Repository: pydantic/pydantic
Pull request: More helpful error re wrong `model_json_schema` usage
Fix https://github.com/pydantic/pydantic/issues/8896

Selected Reviewer: @davidhewitt

Problem statement:
AssertionError when calling BaseModel.schema_json()
### Initial Checks

- [X] I confirm that I'm using Pydantic V2

### Description

Pyndantic is asking me to report this trivial bug, so I do :smiley: 

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/holger-local/.local/lib/python3.12/site-packages/pydantic/main.py", line 1287, in schema_json
    cls.model_json_schema(by_alias=by_alias, ref_template=ref_template),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/holger-local/.local/lib/python3.12/site-packages/pydantic/main.py", line 391, in model_json_schema
    return model_json_schema(
           ^^^^^^^^^^^^^^^^^^
  File "/home/holger-local/.local/lib/python3.12/site-packages/pydantic/json_schema.py", line 2176, in model_json_schema
    assert '__pydantic_core_schema__' in cls.__dict__, 'this is a bug! please report it'
AssertionError: this is a bug! please report it
```

### Example Code

```Python
from pydantic import BaseModel
BaseModel.schema_json()
```


### Python, Pydantic & OS Version

```Text
pydantic version: 2.6.1
        pydantic-core version: 2.16.2
          pydantic-core build: profile=release pgo=true
                 install path: /home/user/.local/lib/python3.12/site-packages/pydantic
               python version: 3.12.1 (main, Dec 18 2023, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]
                     platform: Linux-6.7.5-200.fc39.x86_64-x86_64-with-glibc2.38
             related packages: fastapi-0.109.2 pydantic-settings-2.2.1 typing_extensions-4.8.0
                       commit: unknown
```

Hints from issue discussion:
@hsteinhaus,

Thanks for reporting this. `schema_json()`, or the new (not deprecated) version of said function `model_json_schema()` are meant to be called on subclasses of `BaseModel`, not `BaseModel` itself.

Marking this as resolved.
@sydney-runkle Yes, it is completely clear that calling `model_json_schema()` does not make a lot of sense. 

However, when iterating a module containing user models, that can easily happen by accident. And a message "Assertion failed, this is a bug, please report" might not be the adequate response to the user. So I do not really think that the issue is resolved.
Reopening, we can add a more descriptive error for the case when it's being called on `BaseModel`.

Commit message: more descriptive error

Patch to review:
diff --git a/pydantic/json_schema.py b/pydantic/json_schema.py
index 33cdb6eb801..b4814353c37 100644
--- a/pydantic/json_schema.py
+++ b/pydantic/json_schema.py
@@ -2176,9 +2176,14 @@ def model_json_schema(
     Returns:
         The generated JSON Schema.
     """
+    from .main import BaseModel
+
     schema_generator_instance = schema_generator(by_alias=by_alias, ref_template=ref_template)
     if isinstance(cls.__pydantic_validator__, _mock_val_ser.MockValSer):
         cls.__pydantic_validator__.rebuild()
+
+    if cls is BaseModel:
+        raise AttributeError('model_json_schema() must be called on an instance of BaseModel, not BaseModel itself.')
     assert '__pydantic_core_schema__' in cls.__dict__, 'this is a bug! please report it'
     return schema_generator_instance.generate(cls.__pydantic_core_schema__, mode=mode)
 


Code review comments:
diff_hunk: @@ -2176,9 +2176,14 @@ def model_json_schema(
     Returns:
         The generated JSON Schema.
     """
+    from .main import BaseModel
+
     schema_generator_instance = schema_generator(by_alias=by_alias, ref_template=ref_template)
     if isinstance(cls.__pydantic_validator__, _mock_val_ser.MockValSer):
         cls.__pydantic_validator__.rebuild()
+
+    if cls is BaseModel:
+        raise AttributeError('model_json_schema() must be called on an instance of BaseModel, not BaseModel itself.')
path: pydantic/json_schema.py
text: @user1:
```suggestion
        raise AttributeError('model_json_schema() must be called on a subclass of BaseModel, not BaseModel itself.')
```