Last active
April 14, 2026 19:14
-
-
Save ninjudd/a6936ce00b6cfc37d5a75a958435c3aa to your computer and use it in GitHub Desktop.
Functor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const selectTopic = functor({ | |
| prompt: ({ query, topics }: { query: string; topics: string }) => ` | |
| User query: ${query} | |
| Existing topics: | |
| ${topics} | |
| Match an existing topic if one fits, otherwise create a new one. | |
| `, | |
| returns: [ | |
| { | |
| type: 'choose_existing', | |
| description: 'Pick an existing topic that matches', | |
| schema: z.object({ topicId: z.string() }), | |
| }, | |
| { | |
| type: 'create_new', | |
| description: 'Create a new topic when none match', | |
| schema: z.object({ | |
| name: z.string(), | |
| summary: z.string(), | |
| }), | |
| }, | |
| ], | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const extractPatient = functor({ | |
| prompt: ({ text }: { text: string }) => ` | |
| Extract patient info from the text below. | |
| Return whatever fields are present. | |
| ${text} | |
| `, | |
| returns: { | |
| description: 'Extract basic patient demographics if present', | |
| schema: z.object({ | |
| name: z.string().optional(), | |
| birthDate: z.string().optional(), | |
| gender: z.string().optional(), | |
| }), | |
| }, | |
| }); | |
| const patient = await extractPatient({ text: documentText }); |
Author
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need more than one return shape? The prompt stays a typed function, returns becomes an array, and you get a typed discriminated union. Dispatched in prose.
The trick that makes it work: tool calling, used to select and enforce return types.
Each return is a schema plus a short description.
The model thinks it’s choosing an action. We use it to select the shape of the return value.