---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/llm/transformers_js/model_source/functions/withModelSource.md
---

# Function: withModelSource()

```ts
function withModelSource<T>(
  env: TransformersEnvLike,
  hook: TransformersJsModelSource,
  load: () => Promise<T>,
): Promise<T>;
```

Defined in: [src/batteries/llm/transformers\_js/model\_source.ts:158](https://github.com/NHTIO/ADK/blob/v1.20260713.1/src/src/batteries/llm/transformers_js/model_source.ts#L158)

Run `load()` with `hook` installed on `env`, then restore — serialized against every other
`withModelSource` call so concurrent adapter loads never clobber the global `env`. The hook is only
active for the duration of `load()`; after it resolves (or rejects) the prior `env` cache config is
restored even on error.

## Type Parameters

| Type Parameter |
| -------------- |
| `T`            |

## Parameters

| Parameter | Type                                                                                 | Description                                                               |
| --------- | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------- |
| `env`     | `TransformersEnvLike`                                                                | The transformers.js `env` object.                                         |
| `hook`    | [`TransformersJsModelSource`](../../types/type-aliases/TransformersJsModelSource.md) | The resolver to install for this load.                                    |
| `load`    | () => `Promise`<`T`>                                                               | The async load operation (e.g. `pipeline(...)` / `from_pretrained(...)`). |

## Returns

`Promise`<`T`>
