Jump to solution
Verify

The Fix

pip install pydantic==2.7.2

Based on closed pydantic/pydantic issue #9327 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -990,6 +990,10 @@ def __get_pydantic_core_schema__( ) + @classmethod + def __get_pydantic_json_schema__(cls, cs: CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue: + return handler(core_schema.str_schema())
repro.py
# Issue was in FastApi docs generation, but the below produces an error that # looks the same import pydantic pydantic.TypeAdapter(pydantic.ImportString).json_schema()
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\npip install pydantic==2.7.2\nWhen NOT to use: This fix should not be used if the ImportString type is not required for JSON schema generation.\n\nOption C — Workaround\nwas to create a type-annotated version:\nWhen NOT to use: This fix should not be used if the ImportString type is not required for JSON schema generation.\n\n

Why This Fix Works in Production

  • Trigger: ImportString fails to generate JsonSchema - in fastapi openapi docs generation
  • Mechanism: Pydantic could not generate a JsonSchema for the ImportString type due to missing support
  • Why the fix works: Adds JSON-Schema support for the ImportString type, which previously failed to generate a schema. (first fixed release: 2.7.2).
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • Shows up under Python 3.11 in real deployments (not just unit tests).
  • Pydantic could not generate a JsonSchema for the ImportString type due to missing support
  • Production symptom (often without a traceback): ImportString fails to generate JsonSchema - in fastapi openapi docs generation

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“@amitschang, Ah, nice catch. We'll want to add json schema support to the ImportString type. PR welcome!”
@sydney-runkle · 2024-04-28 · source
“@sydney-runkle, thanks for looking! I'll see if I have some time to work on such a PR”
@amitschang · 2024-04-29 · source
“@sydney-runkle , I made a PR - I can't seem to add the necessary labels to it (ready for review and relnotes-fix), but it is…”
@amitschang · 2024-04-29 · source

Failure Signature (Search String)

  • ImportString fails to generate JsonSchema - in fastapi openapi docs generation
  • raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
Copy-friendly signature
signature.txt
Failure Signature ----------------- ImportString fails to generate JsonSchema - in fastapi openapi docs generation raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- ImportString fails to generate JsonSchema - in fastapi openapi docs generation raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')

Minimal Reproduction

repro.py
# Issue was in FastApi docs generation, but the below produces an error that # looks the same import pydantic pydantic.TypeAdapter(pydantic.ImportString).json_schema()

Environment

  • Python: 3.11
  • Pydantic: 2

What Broke

OpenAPI docs generation failed, causing issues with API documentation.

Why It Broke

Pydantic could not generate a JsonSchema for the ImportString type due to missing support

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==2.7.2

When NOT to use: This fix should not be used if the ImportString type is not required for JSON schema generation.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Option C — Workaround Temporary workaround

was to create a type-annotated version:

When NOT to use: This fix should not be used if the ImportString type is not required for JSON schema generation.

Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.

Fix reference: https://github.com/pydantic/pydantic/pull/9344

First fixed release: 2.7.2

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be used if the ImportString type is not required for JSON schema generation.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
  • Upgrade behind a canary and run integration tests against the canary before 100% rollout.

Version Compatibility Table

VersionStatus
2.7.2 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.