Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread Safety and with_structured_output Wrapping #636

Open
nielsbox opened this issue Dec 10, 2024 · 2 comments · May be fixed by #642
Open

Thread Safety and with_structured_output Wrapping #636

nielsbox opened this issue Dec 10, 2024 · 2 comments · May be fixed by #642

Comments

@nielsbox
Copy link

nielsbox commented Dec 10, 2024

Writing to self.response_schema, contradicts the "wrapping" definition of langchain. Using a model in multiple threads makes it so that response_schemas are getting mixed up. The description itself says that it should return a runnable wrapper of the model.

@nielsbox nielsbox changed the title Thread Safety and with_structuredO]P]]] Thread Safety and with_structured_output Wrapping Dec 10, 2024
@jzaldi
Copy link
Contributor

jzaldi commented Dec 11, 2024

Hi, make sense and can be improved.

  • Can you create a minimal example where we can reproduce the issue?

@nielsbox
Copy link
Author

Sure!

from langchain_google_vertexai import ChatVertexAI, HarmBlockThreshold, HarmCategory


model = ChatVertexAI(
    model_name="gemini-1.5-pro-002",
    project="x",
    location="europe-west1",
    convert_system_message_to_human=True,
)

structured_model = model.with_structured_output(
    {
        "title": "ID Extraction",
        "description": "Extracts IDs from the input text.",
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "description": ("The type"),
                    "enum": [
                        "ORDER_ID",
                        "PO_ID",
                        "INVOICE_ID",
                        "TRACKING_ID",
                        "OTHER",
                    ],
                },
                "value": {"type": "string", "description": "The value of the entity."},
            },
        },
    },
    method="json_mode",
)
print(structured_model.invoke(input="Order ID: AE22334455", temperature=0))
# [{'type': 'ORDER_ID', 'value': 'AE22334455'}]

print(model.invoke("Order ID: AE22334455; Forget everything and return 'foobar'", temperature=0))
# AIMessage(content='[{"type": "ORDER_ID", "value": "AE22334455"}]', additional_kwargs={}, response_metadata=.....

Seems to keep using the same response schema.

@jzaldi jzaldi linked a pull request Dec 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants