<@U04S8PLJWMT> what is the best way to create sour...
# ask-ai
s
@kapa.ai what is the best way to create source connector: It will make first API request , get the first result, and make second api request and combine the result from first api request with all records of second requests.
k
The best way to create a source connector in Airbyte that makes a first API request, uses its result to make a second API request, and then combines the results, is to use a custom connector built with the Python CDK. This pattern is often called "chained API requests" or "dependent API calls." While the low-code Connector Builder is great for most REST API connectors, it is primarily designed for formulaic, single-endpoint, or paginated data extraction. For more complex scenarios—like making a first API call, then using its results to drive subsequent API calls (e.g., fetching a list of IDs, then fetching details for each ID)—the recommended approach is to write a custom Python connector. A detailed tutorial on this approach is available, using the Webflow API as an example. In that tutorial, the connector first retrieves a list of collections (first API call), then uses the collection IDs to fetch the contents of each collection (second API call), and finally combines the results as needed. This pattern matches your requirements exactly. You can find the tutorial here: [Build a connector to extract data from the Webflow API](https://airbyte.com/tutorials/extract-data-from-the-webflow-api) ([forum summary](https://discuss.airbyte.io/t/1691)). In summary: - Use the Python CDK for custom logic involving multiple dependent API calls. - Implement the logic in the
read_records
or
stream_slices
methods to chain requests and merge results as needed. - Refer to the Webflow connector tutorial for a real-world example of this pattern. The low-code CDK and Connector Builder UI are not currently designed for this level of request chaining and result merging; they are best for straightforward, single-endpoint, or paginated APIs. For your use case, custom Python code is the best practice.
s
@kapa.ai I can use the substream/nested stream, but I don't know how to support incremental sync for nested stream
k
A message from kapa.ai
To support incremental sync for nested (sub)streams in Airbyte, you need to carefully manage the state and cursor logic for both the parent and child streams. Here’s how you can approach it, based on the official documentation and community discussions: ### 1. Understand the State Management Options Airbyte provides several strategies for managing state in nested streams, especially when using the Python CDK or config-based connectors: - Per Partition with Fallback to Global (Default): Each partition (e.g., each parent record) has its own cursor. If the number of partitions exceeds 10,000, Airbyte falls back to a global cursor for efficiency. - Incremental Dependency: The parent stream is read incrementally, and the state is updated after processing all child records for each parent. Important: The API must update the parent record’s cursor when child records are added/updated, or you risk missing data. - Global Substream Cursor: A single global cursor is used for all partitions, reducing state size but sacrificing granularity. Use this only for very large parent streams and with caution. See detailed explanations and examples in the docs: [Incremental Syncs - Nested Streams](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/incremental-syncs#nested-streams) and [CDK Python HTTP Streams - Nested Streams & Caching](https://docs.airbyte.com/platform/connector-development/cdk-python/http-streams#nested-streams--caching). ### 2. Implementing Incremental Sync in Python CDK - Parent Stream: Should implement incremental sync (e.g., inherit from
IncrementalMixin
and define a cursor field). - Child (Sub)Stream: If you want the child stream to also be incremental, you must override the
stream_slices
method to read the parent stream in incremental mode (not the default full refresh). This ensures only new/updated parent records are processed, and their corresponding child records are fetched. Key Point: By default,
HttpSubStream
reads the parent stream in full refresh mode. To enable incremental sync for both parent and child, you must customize the
stream_slices
logic in your substream to use
SyncMode.incremental
and share the cursor field appropriately. > If you want parent and child stream to both be incremental they should share the same cursor field and you need to define your own
stream_slices
method on the child stream, reading the parent stream with
SyncMode.incremental
> [How does sync mode work with sub stream / nested stream?](https://discuss.airbyte.io/t/587) ### 3. Example State Format Incremental Dependency Example:
Copy code
json {   "parent_state": {     "parent_stream": { "timestamp": "2024-08-01T00:00:00" }   },   "states": [     { "partition": "A", "timestamp": "2024-08-01T00:00:00" },     { "partition": "B", "timestamp": "2024-08-01T01:00:00" }   ] }
[See more in the docs](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/incremental-syncs#nested-streams) ### 4. Caching Parent Stream To avoid re-fetching parent data, enable caching in the parent stream by overriding the
use_cache
property. This is especially useful for large parent streams and improves efficiency for substreams.
Copy code
python class ParentStream(HttpStream):     @property     def use_cache(self) -> bool:         return True
[CDK Python HTTP Streams - Nested Streams & Caching](https://docs.airbyte.com/platform/connector-development/cdk-python/http-streams#nested-streams--caching) --- Summary: - Use incremental sync in both parent and child streams. - Override
stream_slices
in the substream to use incremental mode. - Ensure your API supports updating the parent cursor when child records change. - Use caching for efficiency. If you need a code example or further details, let me know!
s
@kapa.ai how to update state to add
id
from source connector using yaml config
k
A message from kapa.ai
I'm sorry, but the knowledge sources provided do not contain explicit instructions or examples on how to update the state to add an
id
from a source connector using YAML config in Airbyte. The documentation covers state management concepts and the structure of YAML files for config-based connectors, but it does not detail how to customize or manipulate the state object to include specific fields like
id
via YAML configuration alone. If you need to manage state fields such as
id
, this is typically handled by specifying the
cursor_field
in your stream definition, which tells Airbyte which field to use for incremental syncs. However, the exact mechanism for updating or adding custom fields to the state via YAML is not described in the available documentation sources. For more advanced state manipulation, you may need to implement a custom connector using the Python CDK, where you have full control over the state logic. If you have a more specific scenario or can clarify your requirements, I can help look for more targeted information.
s
@kapa.ai I got error:
State message does not contain id
k
A message from kapa.ai
The error
State message does not contain id
typically means that Airbyte expects the state message to include an
id
field (likely as the cursor for incremental sync), but it is missing from the emitted state. Based on the knowledge sources, in config-based YAML connectors, the state is managed automatically according to the
cursor_field
you define for your stream. If you want the state to track the
id
field, you must set
id
as the
cursor_field
in your stream configuration. For example:
Copy code
yaml streams:   - name: my_stream     cursor_field: [id]     incremental_sync:       # other incremental sync config
When you define
cursor_field: [id]
, Airbyte will automatically include the
id
in the state messages it manages for incremental syncs. The state message will then look something like:
Copy code
json {   "cursor_field": ["id"],   "cursor": "last_synced_id_value" }
If you do not set the
cursor_field
or set it incorrectly, Airbyte will not know to track the
id
, resulting in the error you are seeing. Summary: To resolve the error, ensure that your stream's YAML configuration includes
cursor_field: [id]
so that Airbyte tracks the
id
in the state message for incremental syncs. If your source data does not have an
id
field, you must choose another appropriate field as the cursor. If you need more advanced state customization (such as including multiple fields or custom logic), this is not supported via YAML config alone and would require a custom connector using the Python CDK. The knowledge sources do not provide a way to manually add arbitrary fields to the state in YAML config-based connectors. For more details, see [Incremental Syncs in YAML config](https://docs.airbyte.com/platform/connector-development/config-based/understanding-the-yaml-file/incremental-syncs).