<@U04S8PLJWMT> how to make a transformation that t...
# ask-ai
g
@kapa.ai how to make a transformation that transforms "2025-05" to a normal date on first day like: "2025-05-01"
k
To transform a string like "2025-05" (representing a year and month) into a full date string like "2025-05-01" (the first day of that month) in Airbyte, you should use a custom schema type transformation in your Python CDK connector. You can register a custom transformation function with the Transformer, and in that function, detect if the value matches the "YYYY-MM" pattern, then append "-01" to convert it to "YYYY-MM-01". Here’s an example of how you could implement this:
Copy code
python import re from airbyte_cdk.sources.utils.transform import TransformConfig, Transformer from airbyte_cdk.sources.streams.core import Stream  class MyStream(Stream):     transformer = Transformer(TransformConfig.CustomSchemaNormalization)      @transformer.registerCustomTransform     def transform_year_month_to_date(original_value, field_schema):         # Check if the field is a string and matches the "YYYY-MM" pattern         if (             isinstance(original_value, str)             and re.match(r"^\d{4}-\d{2}$", original_value)             and field_schema.get("format") == "date"         ):             return f"{original_value}-01"         return original_value
- This function checks if the value is a string in the "YYYY-MM" format and the schema expects a "date" format. - If so, it appends "-01" to make it a full date string. You can adapt the field_schema check as needed for your use case. This approach is supported by Airbyte’s custom schema type transformation mechanism, which allows you to define such logic for your connector streams [see documentation](https://docs.airbyte.com/platform/next/connector-development/cdk-python/schemas#custom-schema-type-transformation). If you want to combine this with the default type transformation, you can use:
Copy code
python transformer = Transformer(     TransformConfig.DefaultSchemaNormalization | TransformConfig.CustomSchemaNormalization )
so that both default and custom transformations are applied, with the custom one running after the default [see documentation](https://docs.airbyte.com/platform/next/connector-development/cdk-python/schemas#custom-schema-type-transformation). > Note: This solution is for connectors built with the Python CDK and requires you to define the transformation in your stream class.
g
@kapa.ai I mean on the builder using jinja expression, something like: {{ record['ano_mes'].something }}
k
A message from kapa.ai
To transform a string like
"2025-05"
to
"2025-05-01"
using a Jinja expression in Airbyte's connector builder, you can use Jinja's string concatenation directly in your interpolation:
Copy code
jinja {{ record['ano_mes'] ~ '-01' }}
This expression takes the value of
record['ano_mes']
and appends
-01
to it, resulting in
"2025-05-01"
. There is no built-in Jinja filter in Airbyte specifically for this transformation, but Jinja's
~
operator for string concatenation is supported in string interpolation contexts in the builder [see documentation](https://docs.airbyte.com/platform/1.7/connector-development/config-based/advanced-topics/string-interpolation). If you need to ensure the value matches the
"YYYY-MM"
format before appending, you could use a conditional expression, but for most use cases, the above is sufficient. Example usage in a YAML file:
Copy code
yaml date_field: "{{ record['ano_mes'] ~ '-01' }}"
This will output
"2025-05-01"
when
record['ano_mes']
is
"2025-05"
.
g
@kapa.ai How to transform that into date type instead of string?
k
A message from kapa.ai
To transform a value like
"2025-05"
into a date type (not just a string) in the Airbyte Connector Builder using Jinja, you can use the
str_to_datetime
macro provided by Airbyte. This macro converts a string to a datetime object with UTC timezone. You can first concatenate
-01
to your string, then wrap it with `str_to_datetime`:
Copy code
jinja {{ str_to_datetime(record['ano_mes'] ~ '-01') }}
This will produce a date object for
"2025-05-01"
instead of a plain string. The
str_to_datetime
macro is documented as converting a string to a datetime object with UTC timezone [see documentation](https://docs.airbyte.com/platform/1.7/connector-development/config-based/understanding-the-yaml-file/reference#str_to_datetime). Example:
Copy code
yaml date_field: "{{ str_to_datetime(record['ano_mes'] ~ '-01') }}"
This approach ensures the field is treated as a date type in downstream processing, not just as a string.