Skip to content

Commit

Permalink
docs: add reference to dynamic types impl (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxlijin authored Jul 10, 2024
1 parent 0c95452 commit 85dc8ab
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
53 changes: 50 additions & 3 deletions docs/docs/calling-baml/dynamic-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ async def run():

```typescript TypeScript
import TypeBuilder from '../baml_client/type_builder'
import {
b
} from '../baml_client'
import { b } from '../baml_client'

async function run() {
const tb = new TypeBuilder()
Expand Down Expand Up @@ -190,4 +188,53 @@ const tb = new TypeBuilder()
tb.User.addProperty("email", tb.string()).description("The user's email")
```

</CodeGroup>

### Building dynamic types from JSON schema

We have a working implementation of this, but are waiting for a concrete use case to merge it.
Please chime in on [the GitHub issue](https://github.com/BoundaryML/baml/issues/765) if this is
something you'd like to use.

<CodeGroup>

```python python
import pydantic
from baml_client import b

class Person(pydantic.BaseModel):
last_name: list[str]
height: Optional[float] = pydantic.Field(description="Height in meters")

tb = TypeBuilder()
tb.unstable_features.add_json_schema(Person.model_json_schema())

res = await b.ExtractPeople(
"My name is Harrison. My hair is black and I'm 6 feet tall. I'm pretty good around the hoop. I like giraffes.",
{"tb": tb},
)
```

```typescript TypeScript
import 'z' from zod
import 'zodToJsonSchema' from zod-to-json-schema
import { b } from '../baml_client'

const personSchema = z.object({
animalLiked: z.object({
animal: z.string().describe('The animal mentioned, in singular form.'),
}),
hobbies: z.enum(['chess', 'sports', 'music', 'reading']).array(),
height: z.union([z.string(), z.number().int()]).describe('Height in meters'),
})

let tb = new TypeBuilder()
tb.unstableFeatures.addJsonSchema(zodToJsonSchema(personSchema, 'Person'))

const res = await b.ExtractPeople(
"My name is Harrison. My hair is black and I'm 6 feet tall. I'm pretty good around the hoop. I like giraffes.",
{ tb },
)
```

</CodeGroup>
17 changes: 17 additions & 0 deletions engine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

0 comments on commit 85dc8ab

Please sign in to comment.