langchain/libs/core/langchain_core/utils
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
..
__init__.py core[minor]: Add factory for looking up secrets from the env (#25198) 2024-08-08 16:41:58 -04:00
_merge.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
aiter.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
env.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
formatting.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
function_calling.py core[patch]: add additionalProperties recursively to oai function if strict (#25169) 2024-08-23 00:08:58 +00:00
html.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
image.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
input.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
interactive_env.py core[patch]: simple prompt pretty printing (#15968) 2024-01-12 21:08:51 -05:00
iter.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
json_schema.py core[patch]: Fix utils.json_schema.dereference_refs (#24335 KeyError: 400 in JSON schema processing) (#24337) 2024-07-19 13:31:00 -04:00
json.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
loading.py multiple: update removal targets (#25361) 2024-08-14 09:50:39 -04:00
mustache.py core[patch]: Add UP(upgrade) ruff rules (#25358) 2024-08-22 16:29:22 -07:00
pydantic.py core[patch]: fix deprecation pydantic bug (#25204) 2024-08-08 16:39:38 -07:00
strings.py core: docstrings utils update (#24213) 2024-07-15 11:36:00 -04:00
utils.py core[minor], anthropic[patch]: Upgrade @root_validator usage to be consistent with pydantic 2 (#25457) 2024-08-15 20:09:34 +00:00