# Field Injection | Swift AI Autocomplete Docs

> For the complete documentation index, see [llms.txt](https://magicx.ai/llms.txt). A markdown version of any page on this site is available by appending `.md` to its URL path (the homepage is at `/intent-1/index.md`).

Field Injection guide for integrating the AI Autocomplete SDK with Swift. Code examples, setup, and API reference.

Source: https://magicx.ai/intent-1/docs/swift/field-injection

---

Most fields are answered by AI Autocomplete on its own. When you want a field answered from your own data, or with your own component, you inject it and AI Autocomplete keeps guiding the rest of the query.

## How the SDK works by default

Every field you add in Edit Logic is answered from the dropdown. The type you pick for the field decides how.

| Field type | Answered with | Inserted text |
| --- | --- | --- |
| **Text, the default** | A list of options with icons, narrowed as the user types. Most fields are this type, such as cabin, color or location. | The chosen option's text |
| **Date picker** | A calendar where the user taps one date | The chosen date in the field's date format, for example 2026-03-23 |
| **Date range picker** | One calendar where the user picks a start date and an end date | Both dates in the field's date format, joined by its separator, for example 2026-09-11 - 2026-10-13 |
| **Time picker** | A time picker where the user picks a time of day | The chosen time, for example 7:30 PM |
| **Location search** | A search field where the user types a city or address and picks a place | The chosen place, for example Seattle, WA |
| **Yes / No** | Options, like a text field, for a field that is either true or false, such as On sale or Gift wrap | The chosen option's text |

A date field's date format (YYYY-MM-DD unless you change it) and a date range's separator are set per field in Edit Logic.

## If you want to customize or inject a different UI or component

Injection swaps out one field

Mark the field as injected in Edit Logic and hand the SDK a function for it. When that field comes up, the SDK shows whatever your function returns, and AI Autocomplete keeps guiding the rest of the query.

### If you want to show your own information (contacts or other user fields)

Give the SDK a function that returns your list, for example the user's contacts. When the field comes up, the SDK asks your function and shows the results, narrowed as the user types.

### Address autocomplete

To use your own address autocomplete, mark the address field (for example where) as injected. When where comes up, the SDK asks your function for suggestions and shows them in the dropdown, so it works just as your typical address autocomplete does.

### Or anything else

Any field can be injected the same way: a product catalog, order numbers, open time slots, whatever your app knows. Your function returns the options, the SDK shows them, and the rest of the query stays guided by AI Autocomplete.

## How injection works

You mark a field as injected in Edit Logic, then hand the SDK a function for it. Whenever that field becomes the active pill, the SDK asks your function for options instead of using its own. For a travel booking flow with a location field, one round looks like this:

One injected field, start to finish. Solid arrows to and from your app are the calls your optionOverrides function receives and answers; the dashed arrow is the empty-answer path.

1.  The user types, and AI Autocomplete suggests the next field to fill. Here, that is location.
2.  Because location is injected, the SDK calls your function with what the user has typed for it so far ("" at first, then "SF"). Your function can call your own search endpoint.
3.  Whatever you return is shown in the dropdown as-is, so a fuzzy match your endpoint found (Oakland for "SF") is never hidden.
4.  The user picks an option (or types one out in full) and the field completes. AI Autocomplete suggests the next field, and its own options take over again.

## Step 1: Mark the field as injected

AI Autocomplete needs to know which fields you own, so it can suggest them at the right moment and leave their options to you.

1.  Open [Edit Logic](https://ai-autocomplete.com/edit/logic) and answer Yes to the custom data fields question.
2.  Add a field and give it the name you will use in code, for example location.
3.  Pick "I will inject the values at runtime" as its type.
4.  Save. Your product regenerates with the new field, and the SDK surfaces it as a pill whenever the query calls for it.

The key must match the field name exactly

Key your override by the name exactly as typed in Edit Logic, with no renaming or case conversion. A field saved as location is keyed location; one saved as "Pickup City" is keyed "Pickup City". A key that matches no field is silently ignored.

## Step 2: Connect your data

Pass optionOverrides with one entry per injected field. Each entry is a function that:

-   Receives the text the user has typed for that field.
-   Returns the options to show, synchronously.
-   Should read data that is already in memory. Load or prefetch your list ahead of time and filter it here.

**FieldInjection.swift**

```swift
// "location" added as a runtime-injected custom field in Edit Logic.
// The key must match the field name exactly as typed there.
//
// The closure is synchronous: load your airports up front (or keep a
// warm cache) and filter here. "SF" can match SFO and nearby Oakland.
let airports: [Airport] = AirportIndex.shared.all

let configuration = AIAutocompleteController.Configuration(
    apiConfig: .apiKey(.init(apiKey: "pk_v1_your_public_key")),
    optionOverrides: [
        "location": { query in
            airports
                .filter { query.isEmpty || $0.matches(query) }
                .prefix(8)
                .map { SuggestionOption(text: "\($0.city) (\($0.code))", isTappable: true) }
        }
    ]
)
```

### Fixed and computed lists

Not every injected field needs a request. Return an array directly for a list you already have, or compute options from what the user typed.

**FixedLists.swift**

```swift
optionOverrides: [
    "cabin": { _ in
        ["Economy", "Premium economy", "Business"]
            .map { SuggestionOption(text: $0, isTappable: true) }
    },
    "travelers": { query in
        let digits = query.filter(\.isNumber)
        let label = digits.isEmpty ? "2 travelers" : "\(digits) travelers"
        return [SuggestionOption(text: label, isTappable: true)]
    }
]
```

## What the SDK does with your answer

-   Your function is called the moment the field becomes active, with whatever the user has already typed for it (usually "", the request for the default list). It is called again with each new phrase after the SDK's typing debounce.
-   The list you return is shown as-is. The SDK does not filter it again by the phrase it was produced for, so a match your endpoint found (Oakland for "SF") stays visible.
-   Between two calls, the SDK filters your last answer locally by what the user types, so the dropdown keeps up on every keystroke.
-   While an answer is pending, the dropdown shows its loading state and isLoading is true, the same flag it raises while waiting on AI Autocomplete.
-   Picking an option, or typing an option's text out in full, completes the field. AI Autocomplete then suggests the next field, and its own options take over until another injected field comes up.

## When nothing matches

Return an empty list for something the user typed and the SDK stops waiting on you for that phrase:

-   The typed text is handed to AI Autocomplete, which treats it like any field with no matching options, so the user is never stuck.
-   You are asked again as soon as the phrase changes.
-   An empty list for "" (the default-list request) leaves the field on screen with no options until the user types something.

## Cancellation and errors

-   Keep the closure fast: it runs on the main thread for every phrase. Filter an in-memory list rather than doing work that blocks.
-   A thrown error or rejected promise is contained: it is logged once and treated as an empty answer. It never surfaces as a fetch error or breaks the widget.

## Option shape

Each entry you return is a suggestion option, the same shape AI Autocomplete's own options use.

| Prop | Type | Description |
| --- | --- | --- |
| `text` Required | `String` | Label shown in the dropdown, and inserted into the query when picked. |
| `isTappable` Required | `Bool` | true for an option the user can pick. false renders it as a non-interactive hint (hidden entirely when showNonTappableOptions is false). |
