What is a good way to remove the 'title' field from json schemas - to make them usable in OpenAI chatcompletions calls? #8504
-
The json schema used in example OpenAI chatcompletion calls don't have the
(from https://platform.openai.com/docs/api-reference/chat/create?lang=python) This is not a new idea - it is used in many popular libraries - like https://github.com/jxnl/instructor, https://github.com/minimaxir/simpleaichat and https://github.com/langroid/langroid - they all remove the
from https://github.com/langroid/langroid/blob/main/langroid/utils/pydantic_utils.py#L30 This feels like a hack to me. Aren't there any better methods? I googled for possible answers, found some old documentation and not realizing that it is from an old version I asked a question about using |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I've learned that this is the extended schema format. The OpenAI examples use a minimal schema format. It is not clear if the minimal schema is better - but at least it should require less tokens. |
Beta Was this translation helpful? Give feedback.
-
No way to disable it currently. |
Beta Was this translation helpful? Give feedback.
-
A solution I found to remove titles (might not be bulletproof): from pydantic.json_schema import GenerateJsonSchema
class GenerateJsonSchemaNoTitles(GenerateJsonSchema):
def field_title_should_be_set(self, _) -> bool:
return False
def _update_class_schema(self, json_schema, cls, config) -> None:
super()._update_class_schema(json_schema, cls, config)
json_schema.pop("title", None)
schema_without_titles = SomePydanticClass.model_json_schema(schema_generator=GenerateJsonSchemaNoTitles) Regarding the use case: I actually use it for tool declaration for Gemini and Claude. response = await client.responses.parse(
model=model,
input=messages,
tools=tools_declarations
) to get pydantic objects in the tool calls. Hope I helped someone... |
Beta Was this translation helpful? Give feedback.
No way to disable it currently.