Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document use of SkipJsonSchema for omitting fields #597

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/concepts/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ print(date_range.model_dump_json())
#> {"start_date":"2021-01-01","end_date":"2021-01-30"}
```

## Omitting fields from schema sent to the language model

In some cases, you may wish to have the language model ignore certain fields in your model. You can do this by using Pydantic's `SkipJsonSchema` annotation. This omits a field from the JSON schema emitted by Pydantic (which `instructor` uses for constructing its prompts and tool definitions). For example:

```py
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema

class Response(BaseModel):
question: str
answer: str
private_field: SkipJsonSchema[str | None] = None

assert "private_field" not in Response.model_json_schema()["properties"]
```

Note that because the language model will never return a value for `private_field`, you'll need a default value (this can be a generator via a declared Pydantic `Field`).

## Customizing JSON Schema

There are some fields that are exclusively used to customise the generated JSON Schema:
Expand Down
4 changes: 3 additions & 1 deletion docs/concepts/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Here all docstrings, types, and field annotations will be used to generate the p

## Optional Values

If we use `Optional` and `default`, they will be considered not required when sent to the language model
If we use `Optional` and `default`, they will be considered not required when sent to the language model.

```python
from pydantic import BaseModel, Field
Expand All @@ -43,6 +43,8 @@ class User(BaseModel):
email: Optional[str] = Field(description="The email of the user.", default=None)
```

Note that fields can also be omitted entirely from being sent to the language model by using Pydantic's `SkipJsonSchema` annotation. See [Fields](fields.md#omitting-fields-from-schema-sent-to-the-language-model) for additional details.

## Dynamic model creation

There are some occasions where it is desirable to create a model using runtime information to specify the fields. For this, Pydantic provides the create_model function to allow models to be created on the fly:
Expand Down