Type inference automatically determines data types from available information. DuckDB infers Python user-defined function parameter and return types from annotations, and can infer table column types while reading CSV files. section 6.4 section 10.1.4
Use inference when the input provides enough reliable type information, but make the contract explicit when correctness or portability matters:
- Registering remove_spaces with annotations lets DuckDB infer a VARCHAR parameter and VARCHAR return type. section 6.4
- Reading Stack Overflow CSV files with auto_detect=true infers the table schema directly from the files. section 10.1.4
- Explicit types prevent inference pitfalls when annotations are absent, inaccurate, or unavailable in third-party libraries. section 6.4
- Defining a schema beforehand provides control but requires updates when file structure or column types change. section 10.1.4
Infer or declare a DuckDB function contract
A typed Python function such as def remove_spaces(field: str) -> str: ... can be registered directly. DuckDB exposes the inferred VARCHAR parameter and return type; alternatively, registration can explicitly declare [(VARCHAR)] and VARCHAR. Querying the function against Region confirms the cleaned values and their changed lengths. section 6.4
Listing 10.5 Creating or replacing the Stack Overflow users table
CREATE OR REPLACE TABLE users AS
SELECT *
FROM read_csv(
'Users.csv.gz',
auto_detect=true,
column_names=[
'Id', 'Reputation', 'CreationDate', 'DisplayName',
'LastAccessDate', 'AboutMe', 'Views', 'UpVotes', 'DownVotes'
]
);
Inspect a complete auto-detected CSV table definition for Users, including selected column names and inferred types. section 10.1.4















