{"record":{"id":"ef2bb9ff8ecd0d8d","repo":"apache/beam","slug":"result-violates-schema-explicit-type","errorCode":null,"errorMessage":"{result} violates schema {explicit_type}","messagePattern":"(.+?) violates schema (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/yaml/yaml_mapping.py","lineNumber":427,"sourceCode":"    func = _expand_javascript_mapping_func(original_fields, **expr)\n  elif language in (\"python\", \"generic\", None):\n    func = _expand_python_mapping_func(original_fields, **expr)\n  else:\n    raise ValueError(\n        f'Unknown language for mapping transform: {language}. '\n        'Supported languages are \"javascript\" and \"python.\"')\n\n  if explicit_type:\n    if isinstance(explicit_type, str):\n      explicit_type = {'type': explicit_type}\n    beam_type = json_utils.json_type_to_beam_type(explicit_type)\n    validator = _validator(beam_type)\n\n    @beam.typehints.with_output_types(schemas.typing_from_runner_api(beam_type))\n    def checking_func(row):\n      result = func(row)\n      if not validator(result):\n        raise TypeError(f'{result} violates schema {explicit_type}')\n      return result\n\n    return checking_func\n\n  elif original_type:\n    return beam.typehints.with_output_types(\n        convert_to_beam_type(original_type))(\n            func)\n\n  else:\n    return func\n\n\nclass _StripErrorMetadata(beam.PTransform):\n  \"\"\"Strips error metadata from outputs returned via error handling.\n\n  Generally the error outputs for transformations return information about\n  the error encountered (e.g. error messages and tracebacks) in addition to the","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/yaml/yaml_mapping.py#L409-L445","documentation":"When a MapToFields field declares `output_type`, the resulting checking_func validates every produced value against the declared Beam type at runtime and raises this TypeError if the UDF's result does not satisfy the schema validator. It is a per-element data error, not a config error.","triggerScenarios":"A mapping function returns a value that fails the declared output_type validator at runtime, e.g. declaring output_type: string while the expression returns None or an int; also triggered inside try/except error handling wrappers for failing rows.","commonSituations":"Expressions returning None for missing data (None violates every declared type), int-vs-float mismatches, forgetting that 'number' accepts int/float but 'integer' rejects floats, arrays containing elements of the wrong element_type.","solutions":["Make the UDF coerce its result to the declared type, e.g. `str(x)` or `int(x)`.","Handle None explicitly: use `expression: \"x if x is not None else ''\"` or declare nullable output_type if supported.","Relax or correct the output_type declaration to match what the function actually returns.","Attach error_handling config so bad rows go to an error output instead of failing the pipeline."],"exampleFix":"# before (may return None)\nfields:\n  name:\n    expression: \"row.get('name')\"\n    output_type: string\n# after\nfields:\n  name:\n    expression: \"row.get('name', '')\"\n    output_type: string","handlingStrategy":"try-catch","validationCode":"# sanity-test the UDF against the declared type on sample data\nresult = udf(sample_row)\nassert isinstance(result, str), f'{result!r} does not match output_type'","typeGuard":"def matches_output_type(result, declared: str) -> bool:\n    checks = {'string': str, 'integer': int, 'number': (int, float), 'boolean': bool, 'bytes': bytes}\n    return isinstance(result, checks.get(declared, object)) and result is not None","tryCatchPattern":"config:\n  error_handling:\n    output: errors\n# then consume MyTransform.errors downstream so TypeError rows are captured, not fatal","preventionTips":["Coerce results explicitly in the expression (str(), int())","Guard against None returns with defaults","Enable error_handling on mapping transforms in production","Declare nullable/loose types when data may be missing"],"tags":["yaml","beam-yaml","schema","runtime-validation","type-mismatch"],"backgroundTag":"schema-validation-failed","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"}