<@U04S8PLJWMT> how does airbyte automatically dete...
# ask-ai
m
@kapa.ai how does airbyte automatically detect a schema, what if a field only appears one out of multiple records? How to ensure it’s included?
k
Airbyte automatically detects a schema for schemaless sources (like MongoDB) using dynamic schema inference. During the discovery phase, Airbyte samples a configurable number of records from the source and infers the schema based on the fields and types it observes in those records. For MongoDB, you can set the number of documents to sample (from 1,000 to 10,000, with 10,000 as the default). Airbyte will randomly sample the specified number of documents and build the schema from all top-level fields it finds in those samples. The type assigned to each field is the "widest" type observed (e.g., if a field is sometimes an integer and sometimes a string, it will be typed as a string) [What is a Schemaless Source?](https://docs.airbyte.com/platform/understanding-airbyte/schemaless-sources-and-destinations#what-is-a-schemaless-source). If a field only appears in a small fraction of records (e.g., 1 out of 100,000), there is a risk that it will not be included in the inferred schema, because Airbyte may not sample a record that contains that field. As a result, that field will not be synced to the destination. To ensure a rarely occurring field is included in the schema: - Increase the sample size: In the MongoDB connector, you can increase the "Document discovery sample size" to a larger value (up to the total number of records in your collection). This increases the likelihood that rare fields are observed and included in the schema [[source-Mongodb] schema auto detection is unreliable](https://github.com/airbytehq/airbyte/issues/42862). - Be aware of limitations: Even with a large sample size, if a field is extremely rare, it may still be missed unless you sample all records. This is a known limitation of schema inference by sampling. If your dataset is highly variable or you need to guarantee all fields are included, you may need to consider alternative approaches, such as using the "schemaless" option (which wraps the entire record in a single field), but this comes with trade-offs for downstream SQL-based analytics [What is a Schemaless Source?](https://docs.airbyte.com/platform/understanding-airbyte/schemaless-sources-and-destinations#what-is-a-schemaless-source). In summary: Airbyte's schema detection is based on sampling, so rare fields may be missed unless you increase the sample size or sample all records.
m
@kapa.ai can you reference a nested field as part of a composite key?
mykey
Copy code
"mykey": {
      "myName": "<blah>"
    },
in a response from an api?
k
A message from kapa.ai
Yes, you can reference a nested field as part of a composite key in Airbyte. According to the documentation, the primary key for a stream can be a single field, an array of fields (for a composite key), or an array of arrays representing a composite key where the fields are nested fields. For example, if you have a response like:
Copy code
json {   "mykey": {     "myName": "<blah>"   } }
You would reference the nested field as:
Copy code
yaml primary_key:   - ["mykey", "myName"]
This tells Airbyte to use the value at
mykey.myName
as part of the composite primary key for the record [PrimaryKey - YAML Reference](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/reference#/definitions/PrimaryKey). This approach is supported in the low-code YAML connector definitions and allows you to uniquely identify records using nested fields.
m
@kapa.ai how to add a transformation to bring
mykey
down to the top level record?
k
A message from kapa.ai
To bring the nested field
mykey
up to the top level of the record in Airbyte, you can use the
DpathFlattenFields
transformation. This transformation flattens the specified nested field to the top level of the record. Here’s how you can define it in your stream’s YAML configuration:
Copy code
yaml transformations:   - type: DpathFlattenFields     field_path: ["mykey"]     delete_origin_value: true  # Optional: removes the original nested field
-
field_path: ["mykey"]
specifies the path to the nested field you want to flatten. -
delete_origin_value: true
(optional) will remove the original
mykey
object after flattening, so only its contents remain at the top level. This will move all fields inside
mykey
(e.g.,
myName
) to the top level of the record [Flatten nested fields](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/reference#/definitions/DpathFlattenFields). If you want to keep the original
mykey
as well, you can omit the
delete_origin_value
property or set it to
false
.
m
@kapa.ai I just want to add a new field with the nested value as top level, how to do that?
k
A message from kapa.ai
To add a new top-level field with the value from a nested field, you can use the
AddFields
transformation in your stream definition. Specify the path for the new field at the top level, and set its value using interpolation to reference the nested field. For example, if your record looks like this:
Copy code
json {   "mykey": {     "myName": "<blah>"   } }
And you want to add a new top-level field called
myName_flat
with the value from
mykey.myName
, your transformation would look like:
Copy code
yaml transformations:   - type: AddFields     fields:       - path: ["myName_flat"]         value: "{{ record['mykey']['myName'] }}"
This will result in records like:
Copy code
json {   "mykey": {     "myName": "<blah>"   },   "myName_flat": "<blah>" }
This approach is documented in the Airbyte YAML reference for adding fields using the
AddFields
transformation and referencing nested fields with interpolation [Adding fields](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/record-processing#adding-fields) [Record selector transformations](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/record-selector#transformations).
m
@kapa.ai this seems like the wrong variable for pagination offset increment:
Copy code
{{ next_page_token['next_page_token'] or '0' }}
What’s the correct variable / template variable to use here?
k
A message from kapa.ai
You are correct—when using the OffsetIncrement pagination strategy in Airbyte, the correct variable to use for the offset is not necessarily
{{ next_page_token['next_page_token'] }}
. According to the documentation, the pagination strategies like OffsetIncrement return the number of records read so far as the next page token, and you should use the value directly. For custom parameter injection, the recommended approach is to use the
next_page_token
variable, which is an object with a single key
"next_page_token"
. So, to inject the offset, you would use:
Copy code
jinja {{ next_page_token['next_page_token'] or '0' }}
This is the documented way to access the offset value generated by the OffsetIncrement strategy, especially when you need to combine it with other values or inject it into a custom parameter (e.g., for APIs that require
limit=<offset>,<limit>
). This approach is shown in the documentation for custom parameter injection [Custom parameter injection](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/pagination#custom-parameter-injection). Summary: - For OffsetIncrement, use
{{ next_page_token['next_page_token'] or '0' }}
to access the offset value for pagination in custom parameter injection. - This is the correct and documented template variable for this use case. If you are using the standard pagination form (not custom injection), Airbyte handles this for you. Use the above template only when you need to manually configure the parameter.
m
@kapa.ai offset seems to be incrementing by only 1 in the request, that doesn’t seem like the correct variable to use for offset increment
k
A message from kapa.ai
You are correct to question this behavior. According to the Airbyte documentation, when using the Offset Increment pagination strategy, the offset should be incremented by the number of records returned per page (i.e., the
limit
or
page_size
), not by 1. The offset value should represent the starting index for the next page of results, and it should increase by the page size after each request. Airbyte's Offset Increment strategy is designed to handle this automatically: > your connector will automatically increment the
offset
for subsequent requests based on the number of records it receives, and will continue until it receives fewer records than the limit you configured [Offset Increment Pagination](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/pagination#offset-increment). For example, if your page size (limit) is 2, the requests should look like:
Copy code
GET ...?limit=2&offset=0 GET ...?limit=2&offset=2 GET ...?limit=2&offset=4
and so on. If you observe that the offset is incrementing by only 1, this suggests that the variable or logic being used is not correctly tracking the number of records read so far. The correct variable to use for custom parameter injection is
{{ next_page_token['next_page_token'] or '0' }}
, which, according to the documentation, should represent the total number of records read so far (i.e., the correct offset) [Custom parameter injection](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/pagination#custom-parameter-injection). If the offset is still incrementing by 1, there may be a misconfiguration or a bug. Double-check your pagination configuration and ensure you are using the Offset Increment strategy as described in the documentation. If the issue persists, it may be necessary to review the connector's implementation or consult Airbyte support. In summary: - The offset should increment by the page size, not by 1. - The correct variable is
{{ next_page_token['next_page_token'] or '0' }}
. - If it increments by 1, this is not the expected behavior according to Airbyte's documentation [Offset Increment Pagination](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/pagination#offset-increment).
m
@kapa.ai can you show an example of next page cursor with a nested json http response like: “_blah” : {“next”: “http:///” …
k
A message from kapa.ai
Certainly! If your API response looks like this:
Copy code
json {   "_blah": {     "next": "http://example.com/next-page"     // ... other fields   }   // ... other top-level fields }
And you want to use the value of
"_blah.next"
as the next page cursor in Airbyte, you would configure the cursor extraction in your connector as follows: YAML Example for Cursor Pagination:
Copy code
yaml paginator:   type: "DefaultPaginator"   pagination_strategy:     type: "CursorPagination"     cursor_value: "{{ response['_blah']['next'] }}"   page_token_option:     type: "RequestPath"
This configuration tells Airbyte to extract the
next
field from the nested
_blah
object in the response and use it as the cursor for the next page. The
RequestPath
option means the next request will use the URL provided in that field as the path for the next page request [Cursor paginator in path](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/pagination#cursor). If you need to inject the cursor as a parameter or in another way, you can adjust the
page_token_option
accordingly. Summary: Use
{{ response['_blah']['next'] }}
to reference a nested field as the next page cursor in Airbyte when configuring cursor pagination for a nested JSON response.
m
@kapa.ai can you filter out records before they get pushed to a table?
k
A message from kapa.ai
Yes, you can filter out records before they get pushed to a table in Airbyte. This is accomplished using the Record Filter feature within the Record Selector component. You can define a filter condition (using an interpolated string) that determines which records should be included or excluded. Only records for which the filter expression evaluates to
true
will be emitted by the connector and pushed to the destination table; others will be excluded [Record Filter](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/record-processing#record-filter). For example, to exclude records where
status
is `"expired"`:
Copy code
yaml record_filter:   condition: "{{ record.status != 'expired' }}"
This ensures only relevant records are synced to your destination.
m
@kapa.ai can you add multiple filters?
k
A message from kapa.ai
The knowledge sources do not explicitly mention support for adding multiple separate filters in the Record Filter configuration. However, you can combine multiple conditions into a single filter expression using logical operators such as
and
and
or
. For example:
Copy code
yaml record_filter:   condition: "{{ record.status != 'expired' and record.type == 'active' }}"
This approach allows you to apply multiple filtering criteria within a single filter expression [Record Filter](https://docs.airbyte.com/platform/connector-development/connector-builder-ui/record-processing#record-filter). If you need to apply more complex filtering logic, you can combine conditions as needed within the single
condition
field. There is no documentation indicating support for specifying multiple separate filter blocks.