langchain/libs/core/langchain_core
Yusuke Fukasawa 0258cb96fa
core[patch]: add additionalProperties recursively to oai function if strict (#25169)
Hello. 
First of all, thank you for maintaining such a great project.

## Description
In https://github.com/langchain-ai/langchain/pull/25123, support for
structured_output is added. However, `"additionalProperties": false`
needs to be included at all levels when a nested object is generated.

error from current code:
https://gist.github.com/fufufukakaka/e9b475300e6934853d119428e390f204
```
BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'JokeWithEvaluation': In context=('properties', 'self_evaluation'), 'additionalProperties' is required to be supplied and to be false", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}
```

Reference: [Introducing Structured Outputs in the
API](https://openai.com/index/introducing-structured-outputs-in-the-api/)

```json
{
  "model": "gpt-4o-2024-08-06",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful math tutor."
    },
    {
      "role": "user",
      "content": "solve 8x + 31 = 2"
    }
  ],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "math_response",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "steps": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "explanation": {
                  "type": "string"
                },
                "output": {
                  "type": "string"
                }
              },
              "required": ["explanation", "output"],
              "additionalProperties": false
            }
          },
          "final_answer": {
            "type": "string"
          }
        },
        "required": ["steps", "final_answer"],
        "additionalProperties": false
      }
    }
  }
}
```

In the current code, `"additionalProperties": false` is only added at
the last level.
This PR introduces the `_add_additional_properties_key` function, which
recursively adds `"additionalProperties": false` to the entire JSON
schema for the request.

Twitter handle: `@fukkaa1225`

Thank you!

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
2024-08-23 00:08:58 +00:00
..
_api core[patch]: Improve some error messages and add another test for checking RunnableWithMessageHistory (#25209) 2024-08-22 18:14:27 +00:00
beta core: Updated docstring for Context class (#19079) 2024-03-18 21:15:14 -07:00
callbacks core[patch]: Remove different parent run id warning (#25683) 2024-08-22 16:10:35 -07:00
document_loaders core[patch]: add option to specify the chunk separator in merge_message_runs (#24783) 2024-08-22 19:46:25 +00:00
documents core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
embeddings core[patch]: Update API reference for fake embeddings (#25313) 2024-08-12 19:40:05 +00:00
example_selectors core[patch]: Sort Config attributes (#25127) 2024-08-07 02:53:50 +00:00
graph_vectorstores core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
indexing core[patch]: Fix aindex API (#25155) 2024-08-08 12:08:18 -04:00
language_models core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
load core[patch]: Remove default BaseModel init docstring (#25009) 2024-08-06 01:04:04 +00:00
messages core[patch]: add option to specify the chunk separator in merge_message_runs (#24783) 2024-08-22 19:46:25 +00:00
output_parsers core[patch]: support oai dicts as messages (#25621) 2024-08-21 16:13:15 +00:00
outputs core: Move json parsing in base chat model / output parser to bg thread (#24031) 2024-07-09 12:26:36 -07:00
prompts core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
pydantic_v1 multiple: get rid of pyproject extras (#22581) 2024-06-06 15:45:22 -07:00
runnables core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
tools core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
tracers multiple: update removal targets (#25361) 2024-08-14 09:50:39 -04:00
utils core[patch]: add additionalProperties recursively to oai function if strict (#25169) 2024-08-23 00:08:58 +00:00
vectorstores core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
__init__.py docs: udpated api reference (#25172) 2024-08-14 07:00:17 -07:00
agents.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
caches.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
chat_history.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
chat_loaders.py core: chat_* docstrings (#23412) 2024-06-27 17:29:38 -04:00
chat_sessions.py core: chat_* docstrings (#23412) 2024-06-27 17:29:38 -04:00
env.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
exceptions.py core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
globals.py core[patch]: Fix word spelling error in globals.py (#24532) 2024-07-23 14:27:16 +00:00
memory.py core[patch]: Sort Config attributes (#25127) 2024-08-07 02:53:50 +00:00
prompt_values.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
py.typed core[minor], langchain[patch], experimental[patch]: Added missing py.typed to langchain_core (#14143) 2023-12-01 19:15:23 -08:00
rate_limiters.py core[patch]: use time.monotonic() instead time.time() in InMemoryRateLimiter 2024-08-09 11:31:20 -04:00
retrievers.py multiple: update removal targets (#25361) 2024-08-14 09:50:39 -04:00
stores.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
structured_query.py core[patch]: docstrings langchain_core/ files update (#24285) 2024-07-16 09:21:51 -04:00
sys_info.py core[patch]: Include dependencies in sys_info (#25076) 2024-08-06 09:57:39 -04:00