Skip to content

Commit

Permalink
fix: remove URLs in pydantic error messages
Browse files Browse the repository at this point in the history
Fixes jxnl#509

Pydantic includes error URLs in ValidationErrors which means extra
tokens used in LLM calls.

Pydantic relies on an environment variable to control this setting and
is on by default. https://github.com/pydantic/pydantic-core/blob/e1fc99dd3207157aad77defc20ab6873fd268b5b/python/pydantic_core/_pydantic_core.pyi#L818

This change sets the environment variable dynamically when the package
is invoked.
  • Loading branch information
ssonal committed May 12, 2024
1 parent 1d89f29 commit 5c2a359
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions instructor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import importlib.util

from .mode import Mode
from .utils import set_env_variable
from .process_response import handle_response_model
from .distil import FinetuneFormat, Instructions
from .dsl import (
Expand All @@ -22,6 +23,7 @@
Provider,
)

set_env_variable()

__all__ = [
"Instructor",
Expand Down
5 changes: 5 additions & 0 deletions instructor/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import inspect
import os
import json
import logging
from collections.abc import AsyncGenerator, Generator, Iterable
Expand Down Expand Up @@ -209,3 +210,7 @@ def __init__(self, method: Callable[[Any], R_co]) -> None:

def __get__(self, instance: object, cls: type[Any]) -> R_co:
return self.cproperty(cls)


def set_env_variable():
os.environ["PYDANTIC_ERRORS_INCLUDE_URL"] = "0"
15 changes: 14 additions & 1 deletion tests/test_function_calls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar
from typing import TypeVar, List

import pytest
from anthropic.types import Message, Usage
Expand Down Expand Up @@ -186,3 +186,16 @@ def test_control_characters_allowed_in_anthropic_json_non_strict_mode(
mock_anthropic_message, mode=instructor.Mode.ANTHROPIC_JSON, strict=False
)
assert test_model_instance.data == "Claude likes\ncontrol\ncharacters"


def test_pylance_url_config() -> None:
class Model(BaseModel):
list_of_ints: List[int] = None
a_float: float = None

data = dict(list_of_ints=["1", 2, "bad"], a_float="Not a float")

try:
Model(**data)
except ValidationError as e:
assert "https://errors.pydantic.dev" not in str(e)

0 comments on commit 5c2a359

Please sign in to comment.