Items Iteration
Items allow you to process multiple values in sequence, executing a resource for each item.
Basic Usage
apiVersion: kdeps.io/v1
kind: Resource
metadata:
actionId: processItems
items:
- "Item 1"
- "Item 2"
- "Item 3"
run:
chat:
model: llama3.2:1b
prompt: "Process: {{ get('current') }}"Item Context
When processing items, special getters are available:
| Getter | Description |
|---|---|
get('current') | Current item value |
get('prev') | Previous item (null if first) |
get('next') | Next item (null if last) |
get('index') | Current index (0-based) |
get('count') | Total number of items |
get('all') | Array of all items |
The item Object
You can also access item context through the item object with callable methods:
Method Syntax
run:
expr:
# Method-style access
- set('curr', item.current())
- set('prev', item.prev())
- set('next', item.next())
- set('idx', item.index())
- set('cnt', item.count())
- set('all', item.values())Comparison: get() vs item.method()
| get() Style | item.method() Style | Description |
|---|---|---|
get('current') | item.current() | Current item |
get('prev') | item.prev() | Previous item |
get('next') | item.next() | Next item |
get('index') | item.index() | Current index |
get('count') | item.count() | Total items |
get('all') | item.values() | All items array |
Both syntaxes are equivalent. Use whichever is more readable for your use case.
Example: Using item Object
apiVersion: kdeps.io/v1
kind: Resource
metadata:
actionId: processWithItemObject
items:
- "first"
- "second"
- "third"
run:
expr:
# Using item object methods
- set('position', "Item " + string(item.index() + 1) + " of " + string(item.count()))
- set('hasPrevious', item.prev() != nil)
- set('hasNext', item.next() != nil)
chat:
prompt: |
{{ get('position') }}
Current: {{ item.current() }}
{{ get('hasPrevious') ? 'After: ' + item.prev() : 'First item' }}Accessing All Item Values
After processing, you can access all collected values from a resource that uses items:
Using get('resourceId', 'itemvalues')
apiVersion: kdeps.io/v1
kind: Resource
metadata:
actionId: collectResults
requires:
- processItems
run:
expr:
# Get all collected values from the items iteration
- set('allResults', get('processItems', 'itemvalues'))
- set('resultCount', len(get('allResults')))
apiResponse:
response:
results: get('allResults')
count: get('resultCount')Using item.values(actionID)
You can also use the item.values() method with an action ID to get all iteration values from a specific resource:
apiVersion: kdeps.io/v1
kind: Resource
metadata:
actionId: collectResults
requires:
- processItems
run:
expr:
# Get all values from processItems resource
- set('allResults', item.values('processItems'))
- set('resultCount', len(get('allResults')))
apiResponse:
response:
results: get('allResults')
count: get('resultCount')Note: item.values() without arguments returns all items for the current iteration context (equivalent to item.values() or get('all')). With an action ID, it returns all values from that specific resource's items iteration.
Examples
Simple Processing
items:
- "apple"
- "banana"
- "cherry"
run:
chat:
prompt: |
Item {{ get('index') + 1 }} of {{ get('count') }}: {{ get('current') }}
Describe this fruit.With Context
items:
- "Introduction"
- "Main Content"
- "Conclusion"
run:
chat:
prompt: |
Write the {{ get('current') }} section.
{{ get('prev') ? 'Previous section was: ' + get('prev') : 'This is the first section.' }}
{{ get('next') ? 'Next section will be: ' + get('next') : 'This is the last section.' }}Skip Specific Items
items:
- "process"
- "skip_this"
- "process"
run:
skipCondition:
- get('current') == 'skip_this'
chat:
prompt: "Processing: {{ get('current') }}"Conditional Processing
items:
- value: "Task 1"
priority: "high"
- value: "Task 2"
priority: "low"
- value: "Task 3"
priority: "high"
run:
skipCondition:
- get('current').priority != 'high'
chat:
prompt: "Handle high-priority task: {{ get('current').value }}"Use Cases
Batch LLM Processing
Process multiple queries:
metadata:
actionId: batchQueries
items:
- "What is machine learning?"
- "Explain neural networks"
- "What is deep learning?"
run:
chat:
model: llama3.2:1b
prompt: "{{ get('current') }}"
jsonResponse: true
jsonResponseKeys:
- answerData Enrichment
Enrich a list of records:
metadata:
actionId: enrichProducts
requires: [fetchProducts]
# Items could come from a previous resource
items: get('fetchProducts')
run:
httpClient:
method: GET
url: "https://api.example.com/details/{{ get('current').id }}"Report Generation
Generate sections of a report:
metadata:
actionId: generateReport
items:
- section: "executive_summary"
title: "Executive Summary"
- section: "analysis"
title: "Market Analysis"
- section: "recommendations"
title: "Recommendations"
- section: "conclusion"
title: "Conclusion"
run:
chat:
model: llama3.2:1b
prompt: |
Generate the "{{ get('current').title }}" section of the report.
Data: {{ get('reportData') }}
{{ get('prev') ? 'Previous section: ' + get('prev').title : '' }}
jsonResponse: true
jsonResponseKeys:
- content
- key_pointsMulti-Language Translation
Translate text to multiple languages:
metadata:
actionId: translate
items:
- code: "es"
name: "Spanish"
- code: "fr"
name: "French"
- code: "de"
name: "German"
- code: "ja"
name: "Japanese"
run:
chat:
model: llama3.2:1b
prompt: |
Translate to {{ get('current').name }}:
"{{ get('originalText') }}"
jsonResponse: true
jsonResponseKeys:
- translation
- language_codeSequential Processing
metadata:
actionId: chainedProcess
items:
- step: 1
action: "gather_requirements"
- step: 2
action: "design_solution"
- step: 3
action: "implement"
- step: 4
action: "test"
run:
chat:
prompt: |
Step {{ get('current').step }}: {{ get('current').action }}
{{ get('prev') ? 'Previous step output: ' + get('prev').result : 'Starting fresh.' }}
Complete this step.Image Batch Processing
Process multiple images:
metadata:
actionId: analyzeImages
# Items from file upload or list
items:
- path: "/uploads/image1.jpg"
- path: "/uploads/image2.jpg"
- path: "/uploads/image3.jpg"
run:
chat:
model: llama3.2-vision
prompt: "Describe this image"
files:
- "{{ get('current').path }}"Collecting Results
Item results are collected into an array:
# Processing resource
metadata:
actionId: processItems
items:
- "Item 1"
- "Item 2"
run:
chat:
prompt: "Process {{ get('current') }}"
---
# Response resource
metadata:
actionId: response
requires: [processItems]
run:
apiResponse:
response:
# All results as array
results: get('processItems')
# First result
first: get('processItems')[0]
# Result count
count: len(get('processItems'))Dynamic Items
Items can come from expressions or previous resources:
# From expression
items: split(get('csv_data'), ',')
# From previous resource
items: get('fetchItems')
# Filtered items
items: filter(get('allItems'), .status == 'active')Performance Considerations
- Items process sequentially - Each item waits for the previous to complete
- Set appropriate timeouts - Account for total processing time
- Limit item count - Large lists can be slow
- Consider batching - For very large datasets, use Python for batch processing
Best Practices
- Use skip conditions - Skip items that don't need processing
- Handle errors gracefully - One failing item shouldn't break the batch
- Provide context - Use prev/next for related processing
- Collect results appropriately - Use the right data structure for results
- Consider alternatives - For large batches, Python may be more efficient
Next Steps
- Resources Overview - Resource configuration
- Expressions - Expression syntax
- Expression Functions Reference - Complete function reference
- Python Resource - For complex batch processing