Skip to content

Dataclass ClassVar attributes appear as JSON instance fields in Python prompt schemas #532

Open

Description

@goranai

On main at 20613ef, python_type_to_typescript_schema emits dataclass class variables as instance properties, although dataclasses.fields and the Pydantic-backed validator exclude them. A bare ClassVar also prevents normal translator construction.

from dataclasses import dataclass, fields
from typing import ClassVar
from typechat import TypeChatValidator, python_type_to_typescript_schema

@dataclass
class Request:
    value: int
    shared: ClassVar[str]

print([f.name for f in fields(Request)])  # ['value']
print(TypeChatValidator(Request).validate_object({"value": 1}))  # Success
print(python_type_to_typescript_schema(Request).typescript_schema_str)
# Before includes: shared: string;
# Expected: only value is an instance field.

With shared: ClassVar = 'fake class data', schema conversion reports 'typing.ClassVar' cannot be used as a type annotation. and the normal translator constructor raises. These failures are deterministic before any provider response is needed.

A tested two-line repair skips bare and parameterized ClassVar annotations in the dataclass property loop. Typed, defaulted and inherited class variables then disappear from the generated prompt schema; bare ClassVar classes can construct a translator. Ordinary field validation is retained, including rejection of invalid integer inputs. This changes prompt schemas for the affected dataclasses; it does not change Pydantic validation or class-level data. Other pseudo-fields such as InitVar are outside this repair.

Windows, Python 3.14.7, Pydantic 2.14.3. Latest local result: 749 passed, 22 snapshots; six new cases fail on main and pass with the fix. Independent references: 500 class-variable cases corrected, 500 ordinary cases preserved, zero remaining in-scope errors and zero new rejections. Pyright 1.1.358 reports zero errors. Ruff has 31 existing violating diagnostics and Black still rejects the pre-existing source formatting; the guard increases existing complexity/statement metrics. The new test file passes those style checks. Mypy retains the same three baseline errors. Hosted CI and the other Python/OS versions are unverified.

No exact duplicate found in the recorded all-state searches and nearby maintainer review discussions. Issue #454 concerns runtime wire types, not class variables; PR #364 concerns inline comments. No open PR touches either changed file among all 12 PRs and represented target branches; all 97 remote heads were scanned for an existing repair.

A tested fix is in the linked pull request.

Found and validated with AI assistance (GPT-6.1 via OpenAI Codex); the example above was run again against typechat 0.1.0 from PyPI before filing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions