Convert_pydantic_to_openai_function format

In the Tagging and Extraction chapter of this course, we use pydantic and typing to define the function definition. What I’m noticing is that the function definition of the notebook used in the course is very different from the function definition that I get when I use my own notebook using my own installed version of langchain.

For example, when I run this: print(json.dumps(convert_pydantic_to_openai_function(Information), indent=4)), in the course notebook I get this:

{
    "name": "Information",
    "description": "Information to extract.",
    "parameters": {
        "title": "Information",
        "description": "Information to extract.",
        "type": "object",
        "properties": {
            "people": {
                "title": "People",
                "description": "List of info about people",
                "type": "array",
                "items": {
                    "title": "Person",
                    "description": "Information about a person.",
                    "type": "object",
                    "properties": {
                        "name": {
                            "title": "Name",
                            "description": "person's name",
                            "type": "string"
                        },
                        "age": {
                            "title": "Age",
                            "description": "person's age",
                            "type": "integer"
                        }
                    },
                    "required": [
                        "name"
                    ]
                }
            }
        },
        "required": [
            "people"
        ]
    }
}

In my local notebook using langchain version 0.0.316, I get this:

{
    "name": "Information",
    "description": "Information to extract.",
    "parameters": {
        "$defs": {
            "Person": {
                "description": "Information about a person.",
                "properties": {
                    "name": {
                        "description": "person's name",
                        "title": "Name",
                        "type": "string"
                    },
                    "age": {
                        "anyOf": [
                            {
                                "type": "integer"
                            },
                            {
                                "type": "null"
                            }
                        ],
                        "description": "person's age",
                        "title": "Age"
                    }
                },
                "required": [
                    "name",
                    "age"
                ],
                "title": "Person",
                "type": "object"
            }
        },
        "description": "Information to extract.",
        "properties": {
            "people": {
                "description": "List of info about people",
                "items": {
                    "description": "Information about a person.",
                    "properties": {
                        "name": {
                            "description": "person's name",
                            "title": "Name",
                            "type": "string"
                        },
                        "age": {
                            "anyOf": [
                                {
                                    "type": "integer"
                                },
                                {
                                    "type": "null"
                                }
                            ],
                            "description": "person's age",
                            "title": "Age"
                        }
                    },
                    "required": [
                        "name",
                        "age"
                    ],
                    "title": "Person",
                    "type": "object"
                },
                "title": "People",
                "type": "array"
            }
        },
        "required": [
            "people"
        ],
        "title": "Information",
        "type": "object"
    }
}

Notice the big difference between the 2. My local version of langchain is a lot more verbose, as it includes $def and the shape of age is also very different.

Anyone know why there’s a difference? Both function definitions seem to work, but are there any pitfalls of either? The more verbose one will use up a lot more tokens, so I prefer the more concise one. Any way to get langchain version 0.0.316 to behave like the concise one?

I ran into similar issues. My concern area is with real-world case (extracting the reerenced title, author from a blog post).

  1. I followed the exact code as shown in the course (Tagging and Extracting > Doing it for real).
  2. Instructor’s output shows “title”, “author” whereas my local code returns only “author”. I use
    langchain 0.0.353
    langchain-cli 0.0.21
    langchain-community 0.0.16
    langchain-core 0.1.16
    langchain-openai 0.0.5
    langchainhub 0.1.14
    langserve 0.0.39
    langsmith 0.0.83
    libclang 16.0.6
  3. I nailed the issue to be with convert_pydantic_to_openai_function because the paper_extraction_function (opeai format) does NOT show “title” as “required”. Why?
  4. I suspected that somehow the pydantic-to-openai-conversion is messing up with the generic terms “author” and “title”. So, I updated the pydantic function. Modified the pydantic attributes as: author → paper_author, title → paper_title, and reran the code. Voila! that resolved my issue. Now, the OpenAI function shows “paper_title” as “required”, thereby the invoke returns “paper_author” and “paper_title”. I did not have to use the “prompt” technique at all … the functions did the job adequately.

Overall, I suspect the course and local results are different because the underlying libraries are evolving rapidly.

Hello @mtadanki

I will include you in th DM where the learner issue was addressed. Kindly go through the comments.

I will include you in DM and DM you there. Let me know if you are able to see previous comments in the DM

https://community.deeplearning.ai/t/re-convert-pydantic-to-openai-function-format/491365/3?u=deepti_prasad

Let me know if your issue was not resolved.

Regards
DP

The problem with Title is caused by the implementation of langchain_core.utils.function_calling — :parrot::link: LangChain 0.1.11

In the source code you will find these two lines:

title = schema.pop("title", "")
default_description = schema.pop("description", "")

Pydantic has the title and default_description in the schema definition of the model. Now, if your class has the field title or description, as it happens with the class Paper in the notebook, the field title will be droped. A solution is to rename the field to “t” or any other name that is not title. (don’t use _title. It wont work)