{"record":{"id":"b658458a9a62aa98","repo":"apache/beam","slug":"coder-is-not-of-type-coder","errorCode":null,"errorMessage":"coder is not of type Coder","messagePattern":"coder is not of type Coder","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/userstate.py","lineNumber":54,"sourceCode":"from apache_beam.portability.api import beam_runner_api_pb2\nfrom apache_beam.transforms.timeutil import TimeDomain\nfrom apache_beam.utils import windowed_value\nfrom apache_beam.utils.timestamp import Timestamp\n\nif TYPE_CHECKING:\n  from apache_beam.runners.pipeline_context import PipelineContext\n  from apache_beam.transforms.core import DoFn\n\nCallableT = TypeVar('CallableT', bound=Callable)\n\n\nclass StateSpec(object):\n  \"\"\"Specification for a user DoFn state cell.\"\"\"\n  def __init__(self, name: str, coder: Coder) -> None:\n    if not isinstance(name, str):\n      raise TypeError(\"name is not a string\")\n    if not isinstance(coder, Coder):\n      raise TypeError(\"coder is not of type Coder\")\n    self.name = name\n    self.coder = coder\n\n  def __repr__(self) -> str:\n    return '%s(%s)' % (self.__class__.__name__, self.name)\n\n  def to_runner_api(\n      self, context: 'PipelineContext') -> beam_runner_api_pb2.StateSpec:\n    raise NotImplementedError\n\n\nclass ReadModifyWriteStateSpec(StateSpec):\n  \"\"\"Specification for a user DoFn value state cell.\n     Read more about ReadModifyWriteState (ValueState) here:\n     https://beam.apache.org/documentation/programming-guide/#valuestate\n  \"\"\"\n  def to_runner_api(\n      self, context: 'PipelineContext') -> beam_runner_api_pb2.StateSpec:","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/userstate.py#L36-L72","documentation":"StateSpec.__init__ validates that the coder argument is an instance of apache_beam.coders.Coder. The coder is used to serialize the state cell's values, so arbitrary objects (including None or callables) are rejected with a TypeError.","triggerScenarios":"Calling StateSpec('name', None), passing a raw class instead of an instance (e.g. VarIntCoder instead of VarIntCoder()), or passing some other non-Coder object as the second argument.","commonSituations":"Forgetting the () when constructing a coder (VarIntCoder vs VarIntCoder()); passing None because the coder is 'obvious'; mixing up the coder with a type hint or a CombineFn.","solutions":["Instantiate the coder: use coders.VarIntCoder() not coders.VarIntCoder","Pass an actual Coder instance as the second argument (or use spec classes with default coders where available)","If using CombiningValueStateSpec, note the coder should describe the accumulator type of the CombineFn (often it can be omitted)"],"exampleFix":"// before\nspec = StateSpec('count', VarIntCoder)\n// after\nspec = StateSpec('count', VarIntCoder())","handlingStrategy":"type-guard","validationCode":"from apache_beam.coders import Coder\nif not isinstance(coder, Coder):\n    raise TypeError('coder must be an instantiated Coder')","typeGuard":"from apache_beam.coders import Coder\ndef is_coder(c) -> bool:\n    return isinstance(c, Coder)","tryCatchPattern":"try:\n    spec = StateSpec(name, coder)\nexcept TypeError:\n    spec = StateSpec(name, DefaultCoder())  # fallback to a known-good coder","preventionTips":["Remember coders are instantiated: VarIntCoder(), not VarIntCoder","Pass None or omit only where a spec allows a default coder (e.g. CombiningValueStateSpec derives accumulator coder)","Import coders from apache_beam.coders to ensure they subclass Coder"],"tags":["python","apache-beam","userstate","coder","typeerror"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}