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

Cannot import Url from pydantic.networks #4

Open
tilman-sattler opened this issue Nov 21, 2024 · 2 comments
Open

Cannot import Url from pydantic.networks #4

tilman-sattler opened this issue Nov 21, 2024 · 2 comments

Comments

@tilman-sattler
Copy link

tilman-sattler commented Nov 21, 2024

Description:

Notebook: notebooks/01_a_introduction_to_semantic_kernel.ipynb kann im zweiten Code-Block nicht ausgeführt werden innerhalb des vorbereiteten DevContainers.

Fehlermeldung:

ImportError: cannot import name 'Url' from 'pydantic.networks' (/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/pydantic/networks.py)

Code-Block:

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureTextEmbedding
from semantic_kernel.contents import ChatHistory

kernel = Kernel()

service_id = "chat-gpt"
chat_service = AzureChatCompletion(
    service_id=service_id,
)

embedding_service_id = "embeddings"
kernel.add_service(AzureTextEmbedding(service_id=embedding_service_id))

settings = chat_service.get_prompt_execution_settings_class()
settings.temperature = 0.7
settings.top_p = 0.8

kernel.add_service(chat_service)

# Test the kernel
print("Kernel initialized successfully!")

Trace:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[2], <a href='vscode-notebook-cell:?execution_count=2&line=1'>line 1</a>
----> <a href='vscode-notebook-cell:?execution_count=2&line=1'>1</a> from semantic_kernel import Kernel
      <a href='vscode-notebook-cell:?execution_count=2&line=2'>2</a> from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureTextEmbedding
      <a href='vscode-notebook-cell:?execution_count=2&line=3'>3</a> from semantic_kernel.contents import ChatHistory

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/__init__.py:3
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/__init__.py:1'>1</a> # Copyright (c) Microsoft. All rights reserved.
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/__init__.py:3'>3</a> from semantic_kernel.kernel import Kernel
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/__init__.py:5'>5</a> __version__ = "1.15.0"
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/__init__.py:6'>6</a> __all__ = ["Kernel", "__version__"]

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py:8
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py:5'>5</a> from copy import copy
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py:6'>6</a> from typing import TYPE_CHECKING, Any, Literal, TypeVar
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py:8'>8</a> from semantic_kernel.connectors.ai.embeddings.embedding_generator_base import EmbeddingGeneratorBase
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py:9'>9</a> from semantic_kernel.const import METADATA_EXCEPTION_KEY
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py:10'>10</a> from semantic_kernel.contents.chat_history import ChatHistory

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/__init__.py:3
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/__init__.py:1'>1</a> # Copyright (c) Microsoft. All rights reserved.
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/__init__.py:3'>3</a> from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/__init__.py:5'>5</a> __all__ = ["PromptExecutionSettings"]

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/prompt_execution_settings.py:8
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/prompt_execution_settings.py:4'>4</a> from typing import Any, TypeVar
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/prompt_execution_settings.py:6'>6</a> from pydantic import Field, model_validator
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/prompt_execution_settings.py:8'>8</a> from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/prompt_execution_settings.py:9'>9</a> from semantic_kernel.kernel_pydantic import KernelBaseModel
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/prompt_execution_settings.py:11'>11</a> logger = logging.getLogger(__name__)

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_choice_behavior.py:10
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_choice_behavior.py:6'>6</a> from typing import TYPE_CHECKING, Literal, TypeVar
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_choice_behavior.py:8'>8</a> from typing_extensions import deprecated
---> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_choice_behavior.py:10'>10</a> from semantic_kernel.connectors.ai.function_calling_utils import _combine_filter_dicts
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_choice_behavior.py:11'>11</a> from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_choice_behavior.py:12'>12</a> from semantic_kernel.kernel_pydantic import KernelBaseModel

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_calling_utils.py:6
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_calling_utils.py:3'>3</a> from collections import OrderedDict
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_calling_utils.py:4'>4</a> from typing import TYPE_CHECKING, Any
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_calling_utils.py:6'>6</a> from semantic_kernel.contents.chat_message_content import ChatMessageContent
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_calling_utils.py:7'>7</a> from semantic_kernel.contents.function_result_content import FunctionResultContent
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/function_calling_utils.py:8'>8</a> from semantic_kernel.contents.utils.author_role import AuthorRole

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/__init__.py:3
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/__init__.py:1'>1</a> # Copyright (c) Microsoft. All rights reserved.
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/__init__.py:3'>3</a> from semantic_kernel.contents.annotation_content import AnnotationContent
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/__init__.py:4'>4</a> from semantic_kernel.contents.audio_content import AudioContent
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/__init__.py:5'>5</a> from semantic_kernel.contents.chat_history import ChatHistory

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/annotation_content.py:10
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/annotation_content.py:7'>7</a> from pydantic import Field
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/annotation_content.py:9'>9</a> from semantic_kernel.contents.const import ANNOTATION_CONTENT_TAG, ContentTypes
---> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/annotation_content.py:10'>10</a> from semantic_kernel.contents.kernel_content import KernelContent
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/annotation_content.py:11'>11</a> from semantic_kernel.utils.experimental_decorator import experimental_class
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/annotation_content.py:13'>13</a> logger = logging.getLogger(__name__)

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/kernel_content.py:8
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/kernel_content.py:4'>4</a> from typing import Any, TypeVar
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/kernel_content.py:6'>6</a> from pydantic import Field
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/kernel_content.py:8'>8</a> from semantic_kernel.kernel_pydantic import KernelBaseModel
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/kernel_content.py:10'>10</a> _T = TypeVar("_T", bound="KernelContent")
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/contents/kernel_content.py:13'>13</a> class KernelContent(KernelBaseModel, ABC):

File /workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py:7
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py:4'>4</a> from typing import Annotated, Any, ClassVar, TypeVar
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py:6'>6</a> from pydantic import BaseModel, ConfigDict, Field, UrlConstraints
----> <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py:7'>7</a> from pydantic.networks import Url
      <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py:8'>8</a> from pydantic_settings import BaseSettings, SettingsConfigDict
     <a href='/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py:10'>10</a> HttpsUrl = Annotated[Url, UrlConstraints(max_length=2083, allowed_schemes=["https"])]

ImportError: cannot import name 'Url' from 'pydantic.networks' (/workspaces/wd-ai-hackathon/.venv/lib/python3.12/site-packages/pydantic/networks.py)

Steps to reproduce:

  1. DevContainer erstellen
  2. Sanity Check in notebooks/00_hello_world.ipynb ausführen
  3. In notebooks/01_a_introduction_to_semantic_kernel.ipynb wechseln
  4. Ersten Code-Block ausführen -> sollte funktionieren
  5. Zweiten Code-Block ausführen -> Fehlermeldung
@tilman-sattler
Copy link
Author

Problem lag an pydantic version.
Wenn version nicht explizit gesetzt wird, dann wird die latest version im Container verwendet.
Hierzu auch ein Issue auf dem offiziellen Repo: microsoft/semantic-kernel#9777
pydantic <= 2.9.2 sollte kein Problem machen.

@redhatpeter
Copy link

in my case, pydantic <= 2.9.2 still occurs the issue.
Name: semantic-kernel
Version: 1.15.0
Name: pydantic
Version: 2.8.2
running on Notebook.

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

No branches or pull requests

2 participants